基础动画
//1.在0.5s内,将_field.alpha 的数值变为1.0
[UIView animateWithDuration:0.5 animations:^{
_field.alpha = 1.0;
}];
//2. 较1,在动画完成后,输出log:animation finished!
[UIView animateWithDuration:0.5 animations:^{
_field.alpha = 1.0;
} completion:^(BOOL finished) {
NSLog(@"animation finished!");
}];
//3. 较2,动画在该行代码执行后,延迟0.5s执行
// options: 动画时间曲线项,常用如下:
// UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
// UIViewAnimationOptionCurveEaseIn = 1 << 16,
// UIViewAnimationOptionCurveEaseOut = 2 << 16,
// UIViewAnimationOptionCurveLinear = 3 << 16,
// UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
// UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
// UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
// UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
[UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
_field.alpha = 1.0;
} completion:^(BOOL finished){
NSLog(@"animation finished!");
}];
//4. 较3,动画附带一定的弹簧效果,IOS7+;
// usingSpringWithDamping: 阻尼率,0.0~1.0,越小振荡效果越明显
// initialSpringVelocity: 用于改变动画的初始速率,比如:_field.在X轴上 会以200pt/s 变化,而你想要的效果为100pt/s, 则可设置其值为0.5; 一般情况下,设置为0(即不做处理)较合适
// 推荐使用该动画方法
[UIView animateWithDuration:0.5 delay:0.5 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
} completion:^(BOOL finished){
NSLog(@"animation finished!");
}];
关键帧动画
//较基础动画,可在一个动画中控制UIView的属性值发生多段变化。
// options: 动画时间曲线项,常用如下:
// UIViewKeyframeAnimationOptionCalculationModeLinear = 0 << 10, // default
// UIViewKeyframeAnimationOptionCalculationModeDiscrete = 1 << 10,
// UIViewKeyframeAnimationOptionCalculationModePaced = 2 << 10,
// UIViewKeyframeAnimationOptionCalculationModeCubic = 3 << 10,
// UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10,
// UIViewKeyframeAnimationOptionAllowUserInteraction = UIViewAnimationOptionAllowUserInteraction, // turn on user interaction while animating
// UIViewKeyframeAnimationOptionBeginFromCurrentState = UIViewAnimationOptionBeginFromCurrentState, // start all views from current value, not initial value
// UIViewKeyframeAnimationOptionRepeat = UIViewAnimationOptionRepeat, // repeat animation indefinitely
// UIViewKeyframeAnimationOptionAutoreverse = UIViewAnimationOptionAutoreverse, // if repeat, run animation back and forth
[UIView animateKeyframesWithDuration:0.5 delay:0.5 options:UIViewKeyframeAnimationOptionCalculationModeDiscrete animations:^{
//关键帧1
// addKeyframeWithRelativeStartTime: 相对起始时间
// relativeDuration: 相对执行时间
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.8 animations:^{
_field.alpha = 1.0;
}];
//关键帧2
[UIView addKeyframeWithRelativeStartTime:0.8 relativeDuration:0.2 animations:^{
_field.alpha = 0.5;
}];
} completion:^(BOOL finished){
NSLog(@"animation finished!");
}];