UIView 动画

 

UIView中,跟动画有关的类有三个:UIViewAnimation(首尾动画)、UIViewAnimationWithBlocks(动画块)、UIViewKeyframeAnimations(关键桢动画)

另外,在UIImageView中还有桢动画可以实现动画效果。

 

一、UIViewAnimation 首尾动画

 

常见方法解析

  + (void)beginAnimations:(nullable NSString *)animationID context:(nullable void *)context;  // 动画开始点的标记

  + (void)commitAnimations;  // 动画结束点的标记

 [UIView beginAnimations:nil context:nil] 和 [UIView commitAnimations] 成对出现

  UIKit直接将动画集成到UIView类中,执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间。

  动画的产生:内部的一些属性发生改变,UIView将为这些改变提供动画支持。

 [UIView beginAnimations:nil context:nil] 和 [UIView commitAnimations] 成对出现,内部允许嵌套若干个这样的对。

 

  // no getters. if called outside animation block, these setters have no effect.

  + (void)setAnimationDelegate:(nullable id)delegate;         

  // 设置动画代理对象,当动画开始或者结束时会发消息给代理对象

  + (void)setAnimationWillStartSelector:(nullable SEL)selector;       

  // default = NULL. 当动画即将开始时,执行delegate对象的selector. 

  + (void)setAnimationDidStopSelector:(nullable SEL)selector;         

  // default = NULL. 当动画结束时,执行delegate对象的selector. 

  + (void)setAnimationDuration:(NSTimeInterval)duration;             

  // default = 0.2. 动画的持续时间,秒为单位

  + (void)setAnimationDelay:(NSTimeInterval)delay;                     

  // default = 0.0. 动画延迟delay秒后再开始

  + (void)setAnimationStartDate:(NSDate *)startDate;                  

  // default = now ([NSDate date]).  动画的开始时间,默认为now

  + (void)setAnimationCurve:(UIViewAnimationCurve)curve;           

  // default = UIViewAnimationCurveEaseInOut.  动画的节奏控制

  + (void)setAnimationRepeatCount:(float)repeatCount;                  

  // default = 0.0.  动画的重复次数,可以是小数.

  + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;   

  // default = NO. 重复次数非零时才有效. 如果设置为YES,代表动画每次重复执行的效果会跟上一次相反

  + (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; 

  //设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好

 

其他方法:

  + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;  // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).

  + (void)setAnimationsEnabled:(BOOL)enabled;                         // ignore any attribute changes while set.

  + (BOOL)areAnimationsEnabled;

  + (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation NS_AVAILABLE_IOS(7_0);

  + (NSTimeInterval)inheritedAnimationDuration NS_AVAILABLE_IOS(9_0);

 

 

 二、UIViewAnimationWithBlocks   动画块

 

 1.视图动画

 

  + (void)animateWithDuration:(NSTimeInterval)duration     

                delay:(NSTimeInterval)delay         

                 options:(UIViewAnimationOptions)options  

             animations:(void (^)(void))animations     

             completion:(void (^ __nullable)(BOOL finished))completion; 

  • 属性分析
  •  duration:动画的持续时间
  •  delay:动画延迟delay秒后开始
  •  options:动画的节奏控制
  •  animations:将改变视图属性的代码放在这个block中
  •  completion:动画结束后,会自动调用这个block

 

2.弹簧动画 Spring Animation

 

  •  NS_AVAILABLE_IOS(7_0)
  • 使用弹簧运动(阻尼运动)来描述动画。Spring Animation 的 API 和一般动画相比多了两个参数,分别是usingSpringWithDampinginitialSpringVelocity
  • Spring Animation 前期速度增加得更快,在动画时间一定的前提下,给人感觉更加快速、干净。

+ (void)animateWithDuration:(NSTimeInterval)duration

              delay:(NSTimeInterval)delay

  usingSpringWithDamping:(CGFloat)dampingRatio

       initialSpringVelocity:(CGFloat)velocity

             options:(UIViewAnimationOptions)options

           animations:(void (^)(void))animations

           completion:(void (^ __nullable)(BOOL finished))completion; 

usingSpringWithDamping 参数:阻尼比

  范围为(0.0f1.0f),数值越小「弹簧」的振动效果越明显,越大运动越平稳。

  initialSpringVelocity = 0.0fusingSpringWithDamping:

initialSpringVelocity 参数:初始速度

  initialSpringVelocity则表示初始的速度,数值越大一开始移动越快。

  值得注意的是,初始速度取值较高而时间较短时,也会出现反弹情况。

  usingSpringWithDamping = 1.0finitialSpringVelocity:

 /* Performs `animations` using a timing curve described by the motion of a spring. When `dampingRatio` is 1, the animation will smoothly decelerate to its final model values without oscillating. Damping ratios less than 1 will oscillate more and more before coming to a complete stop. You can use the initial spring velocity to specify how fast the object at the end of the simulated spring was moving before it was attached. It's a unit coordinate system, where 1 is defined as travelling the total animation distance in a second. So if you're changing an object's position by 200pt in this animation, and you want the animation to behave as if the object was moving at 100pt/s before the animation started, you'd pass 0.5. You'll typically want to pass 0 for the velocity. */ 

 

3.转场动画

 

  • 效果一:

    + (void)transitionWithView:(UIView *)view           

                  duration:(NSTimeInterval)duration

                options:(UIViewAnimationOptions)options  

              animations:(void (^ __nullable)(void))animations

              completion:(void (^ __nullable)(BOOL finished))completion;

    属性分析:

    view:需要进行转场动画的视图

    options:转场动画的类型

 

  •  效果二:视图切换:fromView的父视图 删除 fromView , 添加 toView

    + (void)transitionFromView:(UIView *)fromView   

                  toView:(UIView *)toView     

                 duration:(NSTimeInterval)duration

                 options:(UIViewAnimationOptions)options    

               completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

    属性分析:

    FromView:要删除的子视图

    toView:要添加的子视图

 

 4.系统动画

 

+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<__kindof UIView *> *)views options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))parallelAnimations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

Performs the requested system-provided animation on one or more views. Specify addtional animations in the parallelAnimations block. These additional animations will run alongside the system animation with the same timing and duration that the system animation defines/inherits. Additional animations should not modify properties of the view on which the system animation is being performed. Not all system animations honor all available options.

 

三、UIViewKeyframeAnimations  关键桢动画

 

  • IOS7以后苹果新加了这些方法,可以创建更多更复杂更酷炫的动画效果,而不需要去使用到核心动画(CoreAnimation)。

  + (void)animateKeyframesWithDuration:(NSTimeInterval)duration

                         delay:(NSTimeInterval)delay

                        options:(UIViewKeyframeAnimationOptions)options

                      animations:(void (^)(void))animations

                      completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

 

    “它们自身会根据动画总持续时长自动匹配其运行时长”

  + (void)addKeyframeWithRelativeStartTime:(double)frameStartTime     

                   relativeDuration:(double)frameDuration   

                        animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0); 

    属性分析:

    RelativeStartTime:动画相对开始的时间

    relativeDuration:动画相对持续的时间

 

  • 应用实例:彩虹变换视图(同时用到上面这两个类方法)
    • 重点理解下面对『动画相对开始的时间』的应用

    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    
    [UIView animateKeyframesWithDuration:4.0
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionCalculationModeCubic |
     UIViewAnimationOptionCurveLinear
                              animations:^{
         // 创建颜色数组
         NSArray *arrayColors = @[[UIColor orangeColor],
                                  [UIColor yellowColor],
                                  [UIColor greenColor],
                                  [UIColor blueColor],
                                  [UIColor purpleColor],
                                  [UIColor redColor]];
         NSUInteger colorCount = [arrayColors count];
         // 循环添加关键帧
         for (NSUInteger i = 0; i < colorCount; i++) {
             [UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount
                                     relativeDuration:1 / (CGFloat)colorCount
                                           animations:^{
                                               [view setBackgroundColor:arrayColors[i]];
                                           }];
         }

     } completion:^(BOOL finished) {
         
     }];

 

四、imageView的桢动画

  • 可以让一系列的图片在特定的时间内按顺序显示

  相关属性解析:

  animationImages:要显示的图片(一个装着UIImage的NSArray)

  animationDuration:完整地显示一次animationImages中的所有图片所需的时间

  animationRepeatCount:动画的执行次数(默认为0,代表无限循环)

  相关方法解析:

  - (void)startAnimating; 开始动画

  - (void)stopAnimating;  停止动画

  - (BOOL)isAnimating;  是否正在运行动画

  实例:汤姆猫动画

    NSMutableArray *images = [NSMutableArray array];
    
    //添加图片
    for (int i = 0; i < 81; i++)
    {
        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"drink_%02d",i]]];
    }
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:imageView];
    
    imageView.image = images[0];
    //设置图片
    imageView.animationImages = images;
    //动画时间
    imageView.animationDuration = images.count * 0.1;
    //循环次数
    imageView.animationRepeatCount = 0;
    //启动动画
    [imageView startAnimating];

 

posted on 2016-03-27 20:46  Wilson_CYS  阅读(383)  评论(0编辑  收藏  举报

导航