美文网首页iOS Developer
快速上手UIView动画

快速上手UIView动画

作者: 洛洛爱吃肉 | 来源:发表于2017-01-06 16:38 被阅读75次

    UIView动画有两种使用方法

    • UIView [begin commit]模式
      //动画开始标记
       [UIView beginAnimations:@"changeframe" context:nil];
       //动画持续时间
       [UIView setAnimationDuration:2.0];
       //动画的代理对象
       [UIView setAnimationDelegate:self];
       //设置动画将开始时代理对象执行的SEL
       [UIView setAnimationWillStartSelector:nil];
       //设置动画结束时代理对象执行的SEL
       [UIView setAnimationDidStopSelector:nil];
       //设置动画延迟执行的时间
       [UIView setAnimationDelay:1.0];
       //设置动画的重复次数
       [UIView setAnimationRepeatCount:1];
       //设置动画的曲线
       [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       //设置动画是否继续执行相反的动画
       //[UIView setAnimationRepeatAutoreverses:YES];
       _aniView.frame = CGRectMake(SCREEN_WIDTH-100, SCREEN_HEIGHT/2, 50, 50);
       //结束动画标记
       [UIView commitAnimations];
    
    base1.gif
    • UIView Block调用
      先举个Spring动画的例子
         [UIView animateWithDuration:2.0//动画持续时间
                              delay:1.0//动画延迟执行的时间
             usingSpringWithDamping:0.5//震动效果,范围0~1,数值越小震动效果越明显
              initialSpringVelocity:2.0//初始速度,数值越大初始速度越快
                            options:UIViewAnimationOptionCurveEaseInOut//动画的过渡效果
                         animations:^{
                             //执行的动画
                             _aniView.frame = CGRectMake(SCREEN_WIDTH-100, SCREEN_HEIGHT/2, 50, 50);
                         }
                         completion:^(BOOL finished) {
                             //动画执行完毕后的操作
                         }];
    
    Spring.gif

    Spring动画的看懂了,下面这几个肯定就没问题了

    [UIView animateWithDuration:(NSTimeInterval) //动画持续时间 animations:^{ //执行的动画 }];
    [UIView animateWithDuration:(NSTimeInterval) //动画持续时间 animations:^{ //执行的动画 } completion:^(BOOL finished) { //动画执行完毕后的操作 }];
    [UIView animateWithDuration:(NSTimeInterval) //动画持续时间 delay:(NSTimeInterval) //动画延迟执行的时间 options:(UIViewAnimationOptions) //动画的过渡效果 animations:^{ //执行的动画 } completion:^(BOOL finished) { //动画执行完毕后的操作 }];
    

    两种使用方法介绍完了,再看下刚才没提到的动画

    • Keyframes关键帧动画

      有时候我们需要实现多个连续的动画,或许我们可以在 completion中来实现多个动画的连续,但是我们却可以有更好的选择,那就是 Keyframe Animations(帧动画)。
      举个例子:

        [UIView animateKeyframesWithDuration:5.0 //所有动画完成的总时间
                                       delay:1.0 //延迟执行时间
                                     options:UIViewKeyframeAnimationOptionBeginFromCurrentState //枚举类型 UIViewKeyFrameAnimationOptions
                                  animations:^{ //在这里添加动画
                                      [UIView addKeyframeWithRelativeStartTime:0.0 //指相对于全部动画时间的开始时间。比如总时间为10s,设值为0.0,则此动画就是第0秒开始。取值都在0~1.0之间。
                                                              relativeDuration:0.25 //指相对于全部动画时间的持续时间。比如总时间为10秒,设置为0.25,则此动画所持续的时间就是2.5秒。取值都在0~1.0之间
                                                                    animations:^{
                                                                        _aniView.backgroundColor = [UIColor redColor];
                                                                    }];
                                      [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.4 animations:^{
                                          _aniView.transform = CGAffineTransformMakeRotation(M_PI_4);
                                      }];
                                      [UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
                                          _aniView.backgroundColor = [UIColor yellowColor];
                                      }];
                                      [UIView addKeyframeWithRelativeStartTime:0.51 relativeDuration:0.01 animations:^{
                                          _aniView.transform = CGAffineTransformIdentity;
                                      }];
                                      [UIView addKeyframeWithRelativeStartTime:0.55 relativeDuration:0.45 animations:^{
                                          _aniView.backgroundColor = [UIColor greenColor];
                                      }];
                                  } completion:^(BOOL finished) {//这个动画执行完成后会执行这里
                                      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                          //两秒之后重复执行动画
                                          [self buttonClick3:button];
                                      });
                                  }];
    
    Keyframes.gif
    • CATransition动画
      用于做过渡动画或者转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。
      举个例子:
      1.单个视图的过渡效果
    [UIView transitionWithView:_aniView duration:1.5 options:UIViewAnimationOptionTransitionFlipFromTop|UIViewAnimationOptionOverrideInheritedOptions animations:^{
        _aniView.backgroundColor = [UIColor blueColor];
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
    
    transition1.gif

    2.从旧视图转到新视图的动画效果

     UIImageView * newImageView = [[UIImageView alloc]initWithFrame:_aniView.frame];
            newImageView.image = [UIImage imageNamed:@"xinqin-paopao"];
            [UIView transitionFromView:_aniView toView:newImageView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
                NSLog(@"动画结束");
            }];
    
    transition2.gif

    常用的大概就这么多,也比较简单,关于动画的过渡效果有很多枚举值,适合看代码直接动手尝试来学习.

    相关文章

      网友评论

        本文标题:快速上手UIView动画

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