上次学习了iOS学习笔记09-核心动画CoreAnimation,此次继续学习动画,上次使用的CoreAnimation
不少人感受使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UIView动画封装html
苹果知道图层动画使用麻烦,就为咱们封装到了UIView
里,使咱们能够简单的实现各类动画效果。spring
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))ainimations completion:(void (^)(BOOL finished))completion;
/* 开始动画,UIView的动画方法执行完后动画会停留在终点位置,而不须要进行任何特殊处理 duration:执行时间 delay:延迟时间 options:动画设置,例如自动恢复、匀速运动等 completion:动画完成回调方法 */ [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ _imageView.center = location; } completion:^(BOOL finished) { NSLog(@"Animation end."); }];
上面的方法基本知足大部分基础动画的要求,还有一种比较有趣的动画效果iview
/* 建立弹性动画 damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显 springVelocity:弹性复位的速度 */ [UIView animateWithDuration:5.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{ _imageView.center = location; } completion:^(BOOL finished) { NSLog(@"Animation end."); }];
UIView动画方法中有个options参数,是枚举类型,能够组合使用:ide
/* 常规动画属性设置,能够同时选择多个,用或运算组合 */ UIViewAnimationOptionLayoutSubviews/*< 动画过程当中保证子视图跟随运动 */ UIViewAnimationOptionAllowUserInteraction/*< 动画过程当中容许用户交互 */ UIViewAnimationOptionBeginFromCurrentState/*< 全部视图从当前状态开始运行 */ UIViewAnimationOptionRepeat/*< 重复运行动画 */ UIViewAnimationOptionAutoreverse/*< 动画运行结束后回到起始点 */ UIViewAnimationOptionOverrideInheritedDuration/*< 忽略嵌套动画时间设置 */ UIViewAnimationOptionOverrideInheritedCurve/*< 忽略嵌套动画速度设置 */ UIViewAnimationOptionAllowAnimatedContent/*< 动画过程当中重绘视图,只适合转场动画 */ UIViewAnimationOptionShowHideTransitionViews/*< 视图切换直接隐藏旧视图、显示新视图,只适合转场动画 */ UIViewAnimationOptionOverrideInheritedOptions/*< 不继承父动画设置或动画类型 */ /* 动画速度变化控制,其中选一个进行设置 */ UIViewAnimationOptionCurveEaseInOut/*< 动画先缓慢,后逐渐加速,默认值 */ UIViewAnimationOptionCurveEaseIn/*< 动画逐渐变慢 */ UIViewAnimationOptionCurveEaseOut/*< 动画逐渐加速 */ UIViewAnimationOptionCurveLinear/*< 动画匀速执行 */
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))ainimations completion:(void (^)(BOOL finished))completion;
[UIView animateKeyframesWithDuration:5.0 delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionCurveLinear animations:^{ //第一个关键帧:从0秒开始持续50%的时间,也就是5.0*0.5=2.5秒 [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{ _imageView.center = location1; }]; //第二个关键帧:从50%时间开始持续25%的时间,也就是5.0*0.25=1.25秒 [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{ _imageView.center = location2; }]; //第三个关键帧:从75%时间开始持续25%的时间,也就是5.0*0.25=1.25秒 [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{ _imageView.center = location3; }]; } completion:^(BOOL finished) { NSLog(@"Animation end."); }];
/* 动画模式选择,选择一个 */ UIViewKeyframeAnimationOptionCalculationModeLinear/*< 连续运算模式 */ UIViewKeyframeAnimationOptionCalculationModeDiscreter/*< 离散运算模式 */ UIViewKeyframeAnimationOptionCalculationModePacedr/*< 均匀执行运算模式 */ UIViewKeyframeAnimationOptionCalculationModeCubicr/*< 平滑运算模式 */ UIViewKeyframeAnimationOptionCalculationModeCubicPacedr/*< 平滑均匀运算模式 */
UIView动画如今只支持属性关键帧动画,不支持路径关键帧动画post
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))ainimations completion:(void (^)(BOOL finished))completion;
#pragma mark 转场动画 - (void)transitionAnimation:(BOOL)isNext{ UIViewAnimationOptions option; if (isNext) { option = UIViewAnimationOptionCurveLinear | UIViewAnimationOptionTransitionFlipFromRight; } else { option = UIViewAnimationOptionCurveLinear | UIViewAnimationOptionTransitionFlipFromLeft; } [UIView transitionWithView:_imageView duration:1.0 options:option animations:^{ _imageView.image = [self getImage:isNext]; } completion:nil]; }
/* 转场类型 */ UIViewAnimationOptionTransitionNone/*< 没有转场动画效果 */ UIViewAnimationOptionTransitionFlipFromLeft/*< 从左侧翻转效果 */ UIViewAnimationOptionTransitionFlipFromRight/*< 从右侧翻转效果 */ UIViewAnimationOptionTransitionCurlUp/*< 向后翻页的动画过渡效果 */ UIViewAnimationOptionTransitionCurlDown/*< 向前翻页的动画过渡效果 */ UIViewAnimationOptionTransitionCrossDissolve/*< 溶解消失效果 */ UIViewAnimationOptionTransitionFlipFromTop/*< 从上方翻转效果 */ UIViewAnimationOptionTransitionFlipFromBottom/*< 从底部翻转效果 */
- 使用UIView动画封装的转场动画效果少,这里没法直接使用私有API
- 两个视图之间转场可使用
```