美文网首页
iOS 动画--UIView动画

iOS 动画--UIView动画

作者: yaqiong | 来源:发表于2018-03-05 14:37 被阅读27次

    首先看一下官方文档对UIView动画的一些说明

    Changes to several view properties can be animated—that is, changing the property creates an animation starting at the current value and ending at the new value that you specify. The following properties of the UIView class are animatable:

    • frame
    • bounds
    • center
    • alpha
    • backgroundColor

    改变view的一些属性可以产生动画效果,也就是说,这种动画是从属性的初始值开始,到给定的新值为止。以下几个属性是可动画属性:frame、bounds、center、alpha、backgroundColor

    我们可以使用系统提供的UIView的类方法实现动画效果,通过方法中的参数来设置时长、延迟等值

    + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
    
    + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
    
    + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
    

    duration:动画时长
    delay:延迟时间
    options:动画的展示方式
    animation:需要进行怎样的动画
    completion:动画完成后执行
    另外在iOS7之后,又提供了两个关键帧动画方法:

    + (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); 
    

    以及一些转场动画的方法

    参数

    这些方法,传入不同参数,会得出不同的动画效果:

    option
    • Repeating
      UIViewAnimationOptionRepeat:动画循环执行
      UIViewAnimationOptionAutoreverse:动画执行完之后会反向再执行一次
      如果选择了动画循环执行这一option,则程序不会执行后续的completion block代码块

    • Easing
      这一组option给出了动画加速或减速的规则:
      UIViewAnimationOptionCurveEaseInOut先加速后减速,默认情况
      UIViewAnimationOptionCurveEaseIn由慢到快
      UIViewAnimationOptionCurveEaseOut由快到慢
      UIViewAnimationOptionCurveLinear匀速
      如图所示,修改view的frame,动画执行时间都是3秒,选择了不同的option,效果不同:

      Ease.gif
      同样,修改alpha时(如图是alpha值从1到0.2),效果也类似:
      Ease_alpha.gif
    • Transitioning
      UIViewAnimationOptionTransitionNone 没有效果,默认
      UIViewAnimationOptionTransitionFlipFromLeft 从左翻转效果
      UIViewAnimationOptionTransitionFlipFromRight 从右翻转效果
      UIViewAnimationOptionTransitionCurlUp 从上往下翻页
      UIViewAnimationOptionTransitionCurlDown 从下往上翻页
      UIViewAnimationOptionTransitionCrossDissolve 旧视图溶解过渡到下一个视图
      UIViewAnimationOptionTransitionFlipFromTop 从上翻转效果
      UIViewAnimationOptionTransitionFlipFromBottom 从上翻转效果
      这些参数只能在视图切换的时候使用,比如为一个UIImageView切换图片,移动view时不能使用。所以应该使用transition开头的方法

    NSArray *optionArray  = @[@(UIViewAnimationOptionTransitionNone),
                                  @(UIViewAnimationOptionTransitionFlipFromLeft),
                                  @(UIViewAnimationOptionTransitionFlipFromRight),
                                  @(UIViewAnimationOptionTransitionCurlUp),
                                  @(UIViewAnimationOptionTransitionCurlDown),
                                  @(UIViewAnimationOptionTransitionCrossDissolve),
                                  @(UIViewAnimationOptionTransitionFlipFromTop),
                                  @(UIViewAnimationOptionTransitionFlipFromBottom)];
        for (int i = 0;i < TransitionCount;i ++) {
            UIImageView *subBall = self.imgArray[i];
            [UIView transitionWithView:subBall duration:3 options:[optionArray[i] integerValue] animations:^{
                subBall.image = [UIImage imageNamed:@"leilei1"];
            } completion:nil];
        }
    

    以上代码可以实现如图效果,图片从有小哥哥的一面翻到没有的一面,动画时长3秒:


    transition.gif

    可以看出,切换图片的时候,原来的图片会以view中心为轴翻转,而且系统还自动加上了阴影效果

    • 弹簧效果
      这是个比较有意思的api,可以制作出类似弹簧效果的动画:
    + (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;
    

    主要注意一下参数:

    1. dampingRatio:衰减比例,取值范围为0 -1,因为是衰减比例,所以该值越低震动越强
    2. velocity:初始化速度。值越高速度越快
      如图所示是在velocity不变的情况下,不同dampingRatio的效果:


      spring.gif

    相关文章

      网友评论

          本文标题:iOS 动画--UIView动画

          本文链接:https://www.haomeiwen.com/subject/ckicfftx.html