美文网首页
layer动画总结

layer动画总结

作者: 温水煮青蛙a | 来源:发表于2018-01-03 13:48 被阅读0次

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];
}

相关文章

  • layer动画总结

    layer的属性 条形 波浪 波纹 翻转 画圆圈 动的三角形 网格动画 转圈动画

  • 第六篇:UIView block动画

    在开始学习Core Animation提供的layer的隐式动画和layer的显式动画之前,我们先来总结一下UIV...

  • layer动画属性总结

    当你给一个 CALayer 添加动画的时候,动画其实并没有改变这个 layer 的实际属性。取而代之的,系统会创建...

  • CALayer 和 CAAnimation

    Layer Layer可以绘制的动画动画类型 CALayer position 和 anchorpoint Lay...

  • iOS核心动画 - 隐式动画

    添加layer代码: 执行layer动画: 添加view代码: 执行view动画 事务 Core Animatio...

  • 动画效果

    1.UIView和Layer 动画作用在Layer Layer不响应用户交互 UIView所有的动画都是基于Lay...

  • IOS 动画

    动画 Animation CALayer是动画产生的地方,当将动画添加到Layer层的时候,不能直接修改layer...

  • Layer 动画

    基础 单独的layer 不需要附加到 view 上就能直接显示在屏幕上的单独的 layer 有 AVCaptur...

  • layer动画

    推荐一篇博文:http://www.cocoachina.com/ios/20150521/11874.html

  • layer动画和动画委托的实现

    layer动画的实现 layer动画就是层动画,工作原理和视图动画相同。在开始或者是结束时间改变la ye r的属...

网友评论

      本文标题:layer动画总结

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