iOS学习笔记10-UIView动画

上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UIView动画封装

一、UIView动画

苹果知道图层动画使用麻烦,就为我们封装到了UIView里,使我们可以简单的实现各种动画效果。

1. 基础动画

主要实现方法为:
+ (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.");
}];

上面的方法基本满足大部分基础动画的要求,还有一种比较有趣的动画效果

弹簧动画效果:
/*
  创建弹性动画
  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参数,是枚举类型,可以组合使用:

/* 常规动画属性设置,可以同时选择多个,用或运算组合  */
UIViewAnimationOptionLayoutSubviews/*< 动画过程中保证子视图跟随运动 */          
UIViewAnimationOptionAllowUserInteraction/*< 动画过程中允许用户交互 */
UIViewAnimationOptionBeginFromCurrentState/*< 所有视图从当前状态开始运行 */
UIViewAnimationOptionRepeat/*< 重复运行动画 */                 
UIViewAnimationOptionAutoreverse/*< 动画运行结束后回到起始点 */             
UIViewAnimationOptionOverrideInheritedDuration/*< 忽略嵌套动画时间设置 */ 
UIViewAnimationOptionOverrideInheritedCurve/*< 忽略嵌套动画速度设置 */    
UIViewAnimationOptionAllowAnimatedContent/*< 动画过程中重绘视图,只适合转场动画 */    
UIViewAnimationOptionShowHideTransitionViews/*< 视图切换直接隐藏旧视图、显示新视图,只适合转场动画 */   
UIViewAnimationOptionOverrideInheritedOptions/*< 不继承父动画设置或动画类型 */  

/* 动画速度变化控制,其中选一个进行设置 */    
UIViewAnimationOptionCurveEaseInOut/*< 动画先缓慢,后逐渐加速,默认值 */       
UIViewAnimationOptionCurveEaseIn/*< 动画逐渐变慢 */          
UIViewAnimationOptionCurveEaseOut/*< 动画逐渐加速 */             
UIViewAnimationOptionCurveLinear/*< 动画匀速执行 */            

2. 关键帧动画

主要实现方法为:
+ (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.");
}];
关键帧动画的options多了一些选择:
/* 动画模式选择,选择一个 */
UIViewKeyframeAnimationOptionCalculationModeLinear/*< 连续运算模式 */
UIViewKeyframeAnimationOptionCalculationModeDiscreter/*< 离散运算模式 */
UIViewKeyframeAnimationOptionCalculationModePacedr/*< 均匀执行运算模式 */
UIViewKeyframeAnimationOptionCalculationModeCubicr/*< 平滑运算模式 */
UIViewKeyframeAnimationOptionCalculationModeCubicPacedr/*< 平滑均匀运算模式 */

UIView动画现在只支持属性关键帧动画,不支持路径关键帧动画

3. 转场动画

主要实现方法为:
+ (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];
}
转场动画options多了一些选择:
/* 转场类型 */
UIViewAnimationOptionTransitionNone/*< 没有转场动画效果 */
UIViewAnimationOptionTransitionFlipFromLeft/*< 从左侧翻转效果 */
UIViewAnimationOptionTransitionFlipFromRight/*< 从右侧翻转效果 */
UIViewAnimationOptionTransitionCurlUp/*< 向后翻页的动画过渡效果 */  
UIViewAnimationOptionTransitionCurlDown/*< 向前翻页的动画过渡效果 */   
UIViewAnimationOptionTransitionCrossDissolve/*< 溶解消失效果 */ 
UIViewAnimationOptionTransitionFlipFromTop/*< 从上方翻转效果 */
UIViewAnimationOptionTransitionFlipFromBottom/*< 从底部翻转效果 */
  • 使用UIView动画封装的转场动画效果少,这里无法直接使用私有API
  • 两个视图之间转场可以使用
    ```
  • (void)transitionFromView:(UIView )fromView
    toView:(UIView 
    )toView
    duration:(NSTimeInterval)duration
    options:(UIViewAnimationOptions)options
    completion:(void (^)(BOOL finished))completion;
    ```
  • 默认情况,转出的视图会从父视图移除,转入后重新添加
 
原文地址:https://www.cnblogs.com/ming1025/p/6064488.html