• CALayer CABasicAnimation


    CALayer是UIView可以响应事件。一般来说,layer可以有两种用途:一是对view相关属性的设置,包括圆角、阴影、边框等参数;二是实现对view的动画操控。

    因此对一个view进行core animation动画,本质上是对该view的.layer进行动画操纵。

    1.CALayer常见属性

    新建图层

        CALayer * layer = [CALayer layer];

    图层颜色

        layer.backgroundColor = [UIColor redColor].CGColor;

    图层大小

        layer.bounds = CGRectMake(0, 0, 100, 100);

    图层锚点

        layer.anchorPoint = CGPointMake(0, 0);

    图层位置

        layer.position = self.view.center;

    圆角半径

        layer.cornerRadius = 50; 

    边框宽度

        layer.borderWidth = 2;

    边框颜色

        layer.borderColor = [UIColor blackColor].CGColor;

    添加图层

        [self.view.layer addSublayer:layer];

    2.CALayer有2个非常重要的属性:position和anchorPoint

    @property CGPoint position;

    用来设置CALayer在父层中的位置

    以父层的左上角为原点(0, 0)

    @property CGPoint anchorPoint;

    称为“定位点”、“锚点”

    决定着CALayer身上的哪个点会在position属性所指的位置

    以自己的左上角为原点(0, 0)

    它的x、y取值范围都是0~1,默认值为(0.5, 0.5)

    layer.anchorPoint = CGPointMake(0, 0); 

    layer.anchorPoint = CGPointMake(0.5, 0.5);

    layer.anchorPoint = CGPointMake(1, 1);

    3.改变transform的动画

        旋转

        layer.transform = CATransform3DRotate(_layer.transform, 10/180.0*M_PI, 1, 1, 1);

        放大缩小

        layer.transform = CATransform3DScale(_layer.transform, 2, 2, 2);

        平移

        layer.transform = CATransform3DTranslate(_layer.transform, 10, 10, 0);

    4.CABasicAnimation

         CALayer *Layer = [[CALayer alloc] init];

        Layer.backgroundColor = [UIColor blueColor].CGColor;

        Layer.frame = CGRectMake(100, 100, 50, 50);

        Layer.cornerRadius = 10;

        [self.view.layer addSublayer:scaleLayer];

         

        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

        scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];

        scaleAnimation.toValue = [NSNumber numberWithFloat:1.5];

        scaleAnimation.fillMode = kCAFillModeForwards;最后以什么填充

        scaleAnimation.repeatCount = MAXFLOAT;重复次数

        scaleAnimation.duration = 1;

     

        [Layer addAnimation:scaleAnimation forKey:nil];

    常用的属性有

      transform

      transform.ratation.z

      opacity

      position

    5.CAAnimationGroup

        CALayer *Layer = [[CALayer alloc] init];

        Layer.backgroundColor = [UIColor blueColor].CGColor;

        Layer.frame = CGRectMake(100, 100, 50, 50);

        Layer.cornerRadius = 10;

        [self.view.layer addSublayer:Layer];

        放大

        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

        scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];

        scaleAnimation.toValue = [NSNumber numberWithFloat:1.5];

        渐变

        CABasicAnimation * opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

        opacityAnimation.fromValue = @1;

        opacityAnimation.toValue = @0;

        移动

        CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

        moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];

        旋转

        CABasicAnimation *rotateANimation = [CABasicAnimation animationWithKeyPath:@"transform"];

        rotateANimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];

        rotateANimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(Layer.transform, M_PI, 0, 0, 1)];

        

        CAAnimationGroup * group = [CAAnimationGroup animation];

        group.removedOnCompletion = NO;

        group.fillMode = kCAFillModeForwards;

        group.repeatCount = MAXFLOAT;

        group.duration = 1;

     

        group.animations =@[scaleAnimation,opacityAnimation,moveAnimation,rotateANimation];

        [Layer addAnimation:group forKey:nil];

    6.补充

    再旋转中,上边的写法转换成弧度之后不能转多圈,下面实现转多圈

        CABasicAnimation *zrotateANimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

        zrotateANimation.fromValue = [NSNumber numberWithFloat:0];

        zrotateANimation.toValue = [NSNumber numberWithFloat:M_PI * 4.0];

     

  • 相关阅读:
    11.11 程序员的 1111 种死法
    护航11.11,如何筑牢安全防御系统?
    如何应对大促流量洪峰?揭秘京东技术人的备战手册
    TIOBE 11 月编程语言:Java 首次跌出前二;基于Pytorch的Kornia可微分计算机视觉库开源
    会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
    “开源软件供应链点亮计划
    走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
    深度解读展会场景智能推荐搭建之路 | 会展云技术解读
    第一届“多模态自然语言处理研讨会”精彩回顾(免费获取PPT)
    AI 科学家带你快速 Get 人工智能最热技术
  • 原文地址:https://www.cnblogs.com/huoran1120/p/5157397.html
Copyright © 2020-2023  润新知