layer动画总结

layer的属性

self.aView.layer.position = CGPointMake(self.aView.frame.origin.x+self.aView.frame.size.width, self.aView.frame.origin.y);
    self.aView.layer.anchorPoint = CGPointMake(1, 0);//这是比例的   (0,0) (1,0) (0,1) (1,1)   默认是中心点(0.5,0.5)
    //position和anchorPoint 控制在一个点上   不然会出现瞬移效果
    //这里的position设置在左上点   anchorPoint设置在左上点(1.0)

条形

未命名条形.gif

//条形
-(void)animation1{
    // 1.创建一个复制图层对象,设置复制层的属性
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    
    // 1.1.设置复制图层中子层总数:这里包含原始层
    replicatorLayer.instanceCount = 20;
    // 1.2.设置复制子层偏移量,不包含原始层,这里是相对于原始层的x轴的偏移量
    replicatorLayer.instanceTransform = CATransform3DMakeTranslation(15, 0, 0);
    // 1.3.设置复制层的动画延迟事件
    replicatorLayer.instanceDelay = 0.1;
    // 1.4.设置复制层的背景色,如果原始层设置了背景色,这里设置就失去效果
    replicatorLayer.instanceColor = [UIColor redColor].CGColor;
    // 1.5.设置复制层颜色的偏移量
    replicatorLayer.instanceGreenOffset = 0.05;
    replicatorLayer.instanceRedOffset = 0.05;
    replicatorLayer.instanceBlueOffset = 0.05;
    replicatorLayer.instanceAlphaOffset = 0.05;
    
    // 2.创建一个图层对象  单条柱形 (原始层)
    CALayer *layer = [CALayer layer];
    // 2.1.设置layer对象的位置
    layer.position = CGPointMake(10, 50);
    // 2.2.设置layer对象的锚点
    layer.anchorPoint = CGPointMake(0, 0.5);
    // 2.3.设置layer对象的位置大小
    layer.bounds = CGRectMake(0, 0, 10, 50);
    // 2.5.设置layer对象的颜色
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    
    // 3.创建一个基本动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    // 3.1.设置动画的属性
    basicAnimation.keyPath = @"transform.scale.y";
    // 3.2.设置动画的属性值
    basicAnimation.toValue = @0.01;
    // 3.3.设置动画的重复次数
    basicAnimation.repeatCount = MAXFLOAT;
    // 3.4.设置动画的执行时间
    basicAnimation.duration = 0.5;
    // 3.5.设置动画反转
    basicAnimation.autoreverses = YES;
    
    // 4.将动画添加到layer层上
    [layer addAnimation:basicAnimation forKey:nil];
    
    // 5.将layer层添加到复制层上
    [replicatorLayer addSublayer:layer];
    
    // 6.将复制层添加到view视图层上
    [self.animationView.layer addSublayer:replicatorLayer];
}

波浪

未命名波浪.gif

//波浪
-(void)animation4{
    // 1.创建一个复制图层对象,设置复制层的属性
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    
    // 1.1.设置复制图层中子层总数:这里包含原始层
    replicatorLayer.instanceCount = 3;
    // 1.2.设置复制子层偏移量,不包含原始层,这里是相对于原始层的x轴的偏移量
    replicatorLayer.instanceTransform = CATransform3DMakeTranslation(35, 0, 0);
    // 1.3.设置复制层的动画延迟事件
    replicatorLayer.instanceDelay = 0.2;
    // 1.4.设置复制层的背景色,如果原始层设置了背景色,这里设置就失去效果
    replicatorLayer.instanceColor = [UIColor redColor].CGColor;
    // 1.5.设置复制层颜色的偏移量
    replicatorLayer.instanceGreenOffset = 0.1;
    
    // 2.创建一个图层对象  单条柱形 (原始层)
    CALayer *layer = [CALayer layer];
    // 2.1.设置layer对象的位置
    layer.position = CGPointMake(15, 50);
    // 2.2.设置layer对象的锚点
    layer.anchorPoint = CGPointMake(0.5, 0.5);
    // 2.3.设置layer对象的位置大小
    layer.bounds = CGRectMake(0, 0, 30, 30);
    // 2.5.设置layer对象的颜色
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    layer.cornerRadius = 15;
    layer.masksToBounds = YES;
    
    // 3.创建一个基本动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    // 3.1.设置动画的属性
    basicAnimation.keyPath = @"transform.scale";
    // 3.2.设置动画的属性值
    basicAnimation.fromValue = @1;
    basicAnimation.toValue = @0.01;
    // 3.3.设置动画的重复次数
    basicAnimation.repeatCount = MAXFLOAT;
    // 3.4.设置动画的执行时间
    basicAnimation.duration = 0.6;
    // 3.5.设置动画反转
    basicAnimation.autoreverses = YES;
    
    // 4.将动画添加到layer层上
    [layer addAnimation:basicAnimation forKey:nil];
    
    // 5.将layer层添加到复制层上
    [replicatorLayer addSublayer:layer];
    
    // 6.将复制层添加到view视图层上
    [self.animationView.layer addSublayer:replicatorLayer];
}

波纹

未命名波纹.gif

//波纹
-(void)animation6{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(0, 0, 80, 80);
    shapeLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 80, 80)].CGPath;
    shapeLayer.fillColor = [UIColor orangeColor].CGColor;
    shapeLayer.opacity = 0.0;
    
    CABasicAnimation *alpha = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alpha.fromValue = @(1.0);
    alpha.toValue = @(0.0);
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
    scale.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0)];
    scale.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0)];
    
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[alpha,scale];
    animationGroup.duration = 4.0;
    animationGroup.autoreverses = NO;
    animationGroup.repeatCount = HUGE;
    [shapeLayer addAnimation:animationGroup forKey:@"animationGroup"];
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = CGRectMake(0, 0, 100, 100);
//延迟
    replicatorLayer.instanceDelay = 0.5;
//复制的个数
    replicatorLayer.instanceCount = 8;
    replicatorLayer.instanceGreenOffset = 0.05;
    replicatorLayer.instanceRedOffset = 0.05;
    replicatorLayer.instanceBlueOffset = 0.05;
    replicatorLayer.instanceAlphaOffset = 0.05;
    
    [replicatorLayer addSublayer:shapeLayer];
    [self.animationView.layer addSublayer:replicatorLayer];
}

翻转

未命名翻转.gif

//翻转动画
-(void)animation8{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.frame = CGRectMake(10, 10, 100, 100);
    shapeLayer.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath;
    shapeLayer.fillColor = [UIColor redColor].CGColor;
    
    CABasicAnimation *basicAnima = [CABasicAnimation animationWithKeyPath:@"transform"];
    basicAnima.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(CATransform3DIdentity, 0, 0, 1.0, 0)];
    basicAnima.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(CATransform3DIdentity, M_PI, 0, 1.0, 0)];
    basicAnima.repeatCount = HUGE;
    basicAnima.duration = 1.6;
    [shapeLayer addAnimation:basicAnima forKey:nil];
    [self.animationView.layer addSublayer:shapeLayer];
}

画圆圈

未命名画圆圈.gif

//画圆圈
-(void)animation7{
    //左上角10 10 点   半径50
//        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
//        shapeLayer.frame = CGRectMake(10, 10, 50, 50);
//        shapeLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)].CGPath;
//        shapeLayer.fillColor = [UIColor yellowColor].CGColor;
//        [self.animationView.layer addSublayer:shapeLayer];
    
    
    //基础圆
        CAShapeLayer *baseCircleLayer = [CAShapeLayer layer];
        //路径线条宽度
        baseCircleLayer.lineWidth = 5.0f;
        //填充颜色
        baseCircleLayer.fillColor = [UIColor whiteColor].CGColor;
        // 路径线条的颜色
        baseCircleLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    //圆心在 (100, 100)点
//        baseCircleLayer.frame = CGRectMake(50, 100, 10, 10);
        CGMutablePathRef baseCirclePath = CGPathCreateMutable();
        CGPathAddArc(baseCirclePath, &CGAffineTransformIdentity, 100, 100, 50, 0, M_PI*2, NO);
        baseCircleLayer.path = baseCirclePath;
        [self.animationView.layer addSublayer:baseCircleLayer];
    
        //动圆
        CAShapeLayer *animationCircle = [CAShapeLayer layer];
        //填充颜色
        animationCircle.fillColor = [UIColor whiteColor].CGColor;
        // 路径线条的颜色
        animationCircle.strokeColor = [UIColor blackColor].CGColor;
        //路径线条宽度
        animationCircle.lineWidth = 3.0f;

        CGMutablePathRef animationCirclePath = CGPathCreateMutable();
    //圆心 30 30 半径 30 开始角度 结束角度
        CGPathAddArc(animationCirclePath, &CGAffineTransformIdentity, 30, 30, 30, -M_PI_2,M_PI_2*3 , NO);
        animationCircle.path = animationCirclePath;
        [self.animationView.layer addSublayer:animationCircle];

        CABasicAnimation *circleAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        circleAnimation.duration = 10.0f;//总时间
        circleAnimation.fromValue = @0.0;//开始点 这里是百分比
        circleAnimation.toValue = @1;//速率 默认是1  这里如果是2的话 总用时就是总时间的一半 = 5秒
        circleAnimation.repeatCount = MAXFLOAT;//  次 数
        circleAnimation.fillMode = kCAFillModeForwards;
        circleAnimation.autoreverses = YES;
        [animationCircle addAnimation:circleAnimation forKey:@"strokeEndAnimation"];
}

动的三角形

未命名三角.gif

//三角
-(void)animation5{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(0, 0, 30, 30);
    shapeLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 30, 30)].CGPath;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor blueColor].CGColor;
    shapeLayer.lineWidth = 5;
    
    //动圆
    CABasicAnimation *circleAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    circleAnimation.duration = 10.0f;//总时间
    circleAnimation.fromValue = @0.0;//开始点 这里是百分比
    circleAnimation.toValue = @1;//速率 默认是1  这里如果是2的话 总用时就是总时间的一半 = 5秒
    circleAnimation.repeatCount = MAXFLOAT;//  次 数
    circleAnimation.fillMode = kCAFillModeForwards;
    circleAnimation.autoreverses = YES;
    [shapeLayer addAnimation:circleAnimation forKey:@"strokeEndAnimation"];
    
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D fromValue = CATransform3DRotate(CATransform3DIdentity, 0.0, 0.0, 0.0, 0.0);
    scale.fromValue = [NSValue valueWithCATransform3D:fromValue];
    
    CATransform3D toValue = CATransform3DTranslate(CATransform3DIdentity, 60, 0.0, 0.0);
    toValue = CATransform3DRotate(toValue,120.0*M_PI/180.0, 0.0, 0.0, 1.0);
    
    scale.toValue = [NSValue valueWithCATransform3D:toValue];
    scale.autoreverses = NO;
    scale.repeatCount = HUGE;
    scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scale.duration = 0.8;
    
    
    [shapeLayer addAnimation:scale forKey:@"rotateAnimation"];
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = CGRectMake(0, 0, 30, 30);
//延迟
    replicatorLayer.instanceDelay = 0.0;
//复制个数
    replicatorLayer.instanceCount = 3;
    CATransform3D trans3D = CATransform3DIdentity;
    trans3D = CATransform3DTranslate(trans3D, 60, 0, 0);
    trans3D = CATransform3DRotate(trans3D, 120.0*M_PI/180.0, 0.0, 0.0, 1.0);
    replicatorLayer.instanceTransform = trans3D;
    [replicatorLayer addSublayer:shapeLayer];
    [self.animationView.layer addSublayer:replicatorLayer];
}

网格动画

未命名网格.gif

//网格
-(void)animation3{
    NSInteger column = 3;
    CGFloat between = 5.0;
    CGFloat radius = (100 - between * (column - 1))/column;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(0, 0, radius, radius);
    shapeLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, radius, radius)].CGPath;
    shapeLayer.fillColor = [UIColor purpleColor].CGColor;
    
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
    scale.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0)];
    scale.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.2, 0.2, 0.0)];
    scale.autoreverses = YES;
    scale.repeatCount = HUGE;
    scale.duration = 0.6;
    
    CABasicAnimation *alpha = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alpha.fromValue = @(1.0);
    alpha.toValue = @(0.0);
    
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[scale, alpha];
    animationGroup.duration = 1.0;
    animationGroup.autoreverses = YES;
    animationGroup.repeatCount = HUGE;
    [shapeLayer addAnimation:animationGroup forKey:@"groupAnimation"];
    
    CAReplicatorLayer *replicatorLayerX = [CAReplicatorLayer layer];
    replicatorLayerX.frame = CGRectMake(0, 0, 200, 200);
//延迟
    replicatorLayerX.instanceDelay = 0.3;
//复制个数
    replicatorLayerX.instanceCount = column;
    replicatorLayerX.instanceTransform = CATransform3DTranslate(CATransform3DIdentity, radius+between, 0, 0);
    [replicatorLayerX addSublayer:shapeLayer];
    
    CAReplicatorLayer *replicatorLayerY = [CAReplicatorLayer layer];
    replicatorLayerY.frame = CGRectMake(0, 0, 100, 100);
//延迟
    replicatorLayerY.instanceDelay = 0.3;
//复制个数
    replicatorLayerY.instanceCount = column;
    replicatorLayerY.instanceTransform = CATransform3DTranslate(CATransform3DIdentity, 0, radius+between, 0);
    [replicatorLayerY addSublayer:replicatorLayerX];
    
    [self.animationView.layer addSublayer:replicatorLayerY];
}

转圈动画

未命名转圈.gif

//转圈
-(void)animation2{
    // 1.创建一个复制图层对象,设置复制层的属性
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    
    // 1.1.设置复制图层中子层总数:这里包含原始层
    replicatorLayer.instanceCount = 10;
    // 1.2.设置复制子层偏移量,不包含原始层,这里是相对于原始层的x轴的偏移量
    replicatorLayer.instanceTransform = CATransform3DMakeRotation((2 * M_PI) /10, 0, 0, 1);
    // 1.3.设置复制层的动画延迟事件
    replicatorLayer.instanceDelay = 0.1;
    // 1.4.设置复制层的背景色,如果原始层设置了背景色,这里设置就失去效果
//    replicatorLayer.instanceColor = [UIColor yellowColor].CGColor;
    // 1.5.设置复制层颜色的偏移量
    replicatorLayer.instanceGreenOffset = 0.1;
    
    
    
    replicatorLayer.frame = CGRectMake(0, 0, 200, 200);
    replicatorLayer.preservesDepth = YES;
    replicatorLayer.instanceRedOffset = 0.1;
    replicatorLayer.instanceGreenOffset = 0.1;
    replicatorLayer.instanceBlueOffset = 0.1;
    replicatorLayer.instanceAlphaOffset = 0.1;
//延迟
    replicatorLayer.instanceDelay = 1.0/10;
    
    
    
    
    // 2.创建一个图层对象  单条柱形 (原始层)
    CALayer *layer = [CALayer layer];
    // 2.1.设置layer对象的位置
    layer.position = CGPointMake(100, 50);
    // 2.2.设置layer对象的锚点
    layer.anchorPoint = CGPointMake(0.5, 0.5);
    // 2.3.设置layer对象的位置大小
    layer.bounds = CGRectMake(0, 0, 30, 30);
    // 2.5.设置layer对象的颜色
    layer.backgroundColor = [UIColor yellowColor].CGColor;
    layer.cornerRadius = 15;
    layer.masksToBounds = YES;
    layer.transform = CATransform3DMakeScale(0.01, 0.01, 0.01);

    
    // 3.创建一个基本动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    // 3.1.设置动画的属性
    basicAnimation.keyPath = @"transform.scale";
    // 3.2.设置动画的属性值
    basicAnimation.fromValue = @1;
    basicAnimation.toValue = @0.01;
    // 3.3.设置动画的重复次数
    basicAnimation.repeatCount = MAXFLOAT;
    // 3.4.设置动画的执行时间
    basicAnimation.duration = 1;
    // 3.5.设置动画反转
    basicAnimation.autoreverses = NO;
    
    // 4.将动画添加到layer层上
    [layer addAnimation:basicAnimation forKey:nil];
    
    // 5.将layer层添加到复制层上
    [replicatorLayer addSublayer:layer];
    
    // 6.将复制层添加到view视图层上
    [self.animationView.layer addSublayer:replicatorLayer];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载://www.greatytc.com/p/32fcadd12108 每个UIView有一个伙伴称为l...
    F麦子阅读 11,469评论 0 13
  • 每个UIView有一个伙伴称为layer,一个CALayer。UIView实际上并没有把自己画到屏幕上;它绘制本身...
    shenzhenboy阅读 8,304评论 0 17
  • 原文链接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影阅读 33,034评论 6 472
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,195评论 4 61
  • 今天是个特殊的日子,和往常一样早起看书学习!这个时间是我最放松的时候!并给自己做个美容,试用了很多面膜,我的脸都莫...
    yoga丽阅读 2,934评论 5 4