美文网首页
iOS动画 CoreAnimation

iOS动画 CoreAnimation

作者: harryyu | 来源:发表于2017-07-11 11:57 被阅读40次

    * CoreAnimation*的结构图

    CoreAnimation结构图

    CoreAnimation的几种动画

    • CABasicAnimation 提供了最基础的动画属性设置,是简单的keyframe动画性能,CABasicAnimation可以看做是一种CAKeyframeAnimation的简单动画,因为它只有头尾的关键帧(keyframe)。
    • CAKeyframeAnimation 提供了通用的keyframe动画功能层的属性给我们使用,它允许我们设置一个特定的数组,这个数组是动画在运动时的值,也就是说,CAKeyframeAnimation支持动画的多个值设置。
    • CAAnimationGroup 可以保存一组动画对象,将CAAnimationGroup对象加入图层后,组中所有动画对象可以同时并发运行。
    • CATransition 主要用于做过渡动画或者转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。 其中最主要的就是 type 和 subType 这两个属性。

    iOS Animation-KeyPath

    keyPath值                  说明                   值类型
    position                  移动位置              CGPoint
    opacity                透明度                0.0-1
    bounds                  变大与位置              CGRect
    bounds.size            由小变大             CGSize
    backgroundColor        背景颜色             CGColor
    cornerRadius              渐变圆角              任意数值
    borderWidth            改变边框border的粗细     任意数值
    contents                  改变layer内容(图片)   CGImage
    transform.scale          缩放、放大             0.0-1.0
    transform.rotation.x    旋转动画(翻转,沿着X轴)   M_PI*n
    transform.rotation.y    旋转动画(翻转,沿着Y轴)   M_PI*n
    transform.rotation.z    旋转动画(翻转,沿着Z轴)   M_PI*n
    transform.translation.x 横向移动(沿着X轴)    任意数值
    transform.translation.y 纵向移动(沿着Y轴)    任意数值
    

    CABasicAnimation(基本动画)

    CABasicAnimation类只有三个属性
    @property(nullable, strong) id fromValue; //开始值
    @property(nullable, strong) id toValue;   //结束值
    @property(nullable, strong) id byValue;   //动画的时间
    

    示例代码:下面为Label左侧滑出的动画

      CABasicAnimation * base = [CABasicAnimation animationWithKeyPath:@"position.x"];
        //起始点
        base.fromValue =@(-self.view.bounds.size.width/2);
        //结束点
        base.toValue = @(self.view.bounds.size.width/2) ;
        //时间间隔
        base.duration = 0.7;
       //决定当前对象在非active时间段的行为.比如动画开始之前,动画结束之后
        base.fillMode = kCAFillModeBoth;
        base.delegate = self;
       [self.titleLab.layer addAnimation: base forKey:nil];
    
    

    CAKeyframeAnimation(关键帧动画)

    
    @property(nullable, copy) NSArray *values;  //关键帧数组
    @property(nullable) CGPathRef path;
    @property(nullable, copy) NSArray<NSNumber *> *keyTimes;//关键帧时间点
    
    

    示例代码:下面为Label上下震动动画

      CAKeyframeAnimation * rotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
        rotate.duration = 0.25;   //时间间隔
        rotate.repeatCount = 4;   //重复次数
        rotate.values =  @[@0,@(-M_PI_4/4),@0,@(M_PI_4/4), @0];    //关键帧数组
        rotate.keyTimes = @[@0,@0.25,@0.5,@0.75,@1.0];             //关键帧时间点数组
        [self.titleLab.layer addAnimation:rotate forKey:nil];
    
    

    CAAnimationGroup(组合动画)

    @property(nullable, copy) NSArray<CAAnimation *> *animations; //动画数组
    

    示例代码:给Button添加旋转、渐现、缩放动画

      CAAnimationGroup * group = [[CAAnimationGroup alloc]init];
        group.beginTime = CACurrentMediaTime() + 0.5; //当前时间延迟动画
        group.duration = 0.5;  //动画时间
        group.fillMode = kCAFillModeForwards; //决定当前对象在非active时间段的行为.比如动画开始之前,动画结束之后
        group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];//速度控制函数
        
        CABasicAnimation * scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnim.fromValue = @(3.5);
        scaleAnim.toValue = @(1);
        
        CABasicAnimation * rotateAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotateAnim.fromValue = @(M_PI_4);
        rotateAnim.toValue = @(0);
        
        CABasicAnimation * fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        fadeAnim.fromValue = @(0);
        fadeAnim.toValue = @(1);
        
        group.animations = @[scaleAnim,rotateAnim,fadeAnim];
        [self.longinBtn.layer addAnimation:group forKey:nil];
    
    

    CATransition(转场动画)

    @property(copy) NSString *type;  //详解在下面
    @property(nullable, copy) NSString *subtype; //详解在下面
    @property float startProgress;
    @property float endProgress;
    @property(nullable, strong) id filter;
    
    • Type: 动画类型
      公共Type(官方的SDK其实只提供了四种过渡效果)
    CA_EXTERN NSString * const kCATransitionFade;
    CA_EXTERN NSString * const kCATransitionMoveIn;
    CA_EXTERN NSString * const kCATransitionPush;
    CA_EXTERN NSString * const kCATransitionReveal;
    

    私有的Type (私有API提供了其他很多非常炫的过渡动画),需要自己额外写的

    NSString *const kCATransitionCube = @"cube"; 
    NSString *const kCATransitionSuckEffect = @"suckEffect"; 
    NSString *const kCATransitionOglFlip = @"oglFlip"; 
    NSString *const kCATransitionRippleEffect = @"rippleEffect"; 
    NSString *const kCATransitionPageCurl = @"pageCurl"; 
    NSString *const kCATransitionPageUnCurl = @"pageUnCurl"; 
    NSString *const kCATransitionCameraIrisHollowOpen = @"cameraIrisHollowOpen";
    NSString *const kCATransitionCameraIrisHollowClose = @"cameraIrisHollowClose";
    
    • SubType: 动画类型的方向
    CA_EXTERN NSString * const kCATransitionFromRight;
    CA_EXTERN NSString * const kCATransitionFromLeft;
    CA_EXTERN NSString * const kCATransitionFromTop;
    CA_EXTERN NSString * const kCATransitionFromBottom;
    

    示例代码及效果

      CATransition * transition = [CATransition animation];
        transition.type = kCATransitionCube; ;//设置动画的类型
        transition.subtype = kCATransitionFromRight; //设置动画的方向
        transition.duration = 1.0f;
        [self.testView.layer addAnimation:transition forKey:@"push"];
    
    kCATransitionCube.gif
    注:私有Type,审核的时候官网明确会拒绝

    相关文章

      网友评论

          本文标题:iOS动画 CoreAnimation

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