美文网首页
形状图层和复制图层的简单搭配使用

形状图层和复制图层的简单搭配使用

作者: Z了个L | 来源:发表于2017-02-07 11:18 被阅读34次
    
    // ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    
    @end
    
    
    // ViewController.m
    #import "ViewController.h"
    #import "UIView+Frame.h"
    
    //屏幕宽高
    #define screenW [UIScreen mainScreen].bounds.size.width
    #define screenH [UIScreen mainScreen].bounds.size.height
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    /*
        主要实现思路:
        1.创建形状图层
        2.创建复制图层
        3.创建透明度动画
        4.创建缩放动画
        5.创建动画组把2个动画添加进去
        6.把动画组添加到形状图层
     */
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        [self setUp];
    }
    
    - (void)setUp {
    
        UIView *animBack = [[UIView alloc] init];
        animBack.width = screenW;
        animBack.height = screenW;
        animBack.center = self.view.center;
        [self.view addSubview:animBack];
    
        // 形状图层
        CAShapeLayer *shapL = [CAShapeLayer layer];
        shapL.frame = animBack.bounds;
        shapL.fillColor = [UIColor redColor].CGColor;
        shapL.path = [UIBezierPath bezierPathWithOvalInRect:shapL.bounds].CGPath;
    //    shapL.path = [UIBezierPath bezierPathWithRect:shapL.bounds].CGPath;
        // 这句代码不加的话,程序一起来,就会出现一个很大的红色的圆
        shapL.opacity = 0;
    
        // 复制图层
        CAReplicatorLayer *repL = [CAReplicatorLayer layer];
        repL.frame = animBack.bounds;
        // 复制的份数算自己本身一份
        repL.instanceCount = 3;
        // 每一份显示的间隔时间
        repL.instanceDelay = 0.5;
        [animBack.layer addSublayer:repL];
        // 把形状图层添加到复制层中去
        [repL addSublayer:shapL];
    
        // 添加透明动画
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        anim.fromValue = @(0.3);
        anim.toValue = @(0);
    
        // 添加放大动画
        CABasicAnimation *anim2  = [CABasicAnimation animationWithKeyPath:@"transform"];
        anim2.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 0)];
        anim2.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 0)];
    
        // 创建动画组
        CAAnimationGroup *groupA = [CAAnimationGroup animation];
        groupA.animations = @[anim, anim2];
        groupA.duration = 3.0;
        groupA.repeatCount = HUGE;
        // 给形状图层添加动画
        [shapL addAnimation:groupA forKey:nil];
    }
    
    @end
    
    
    • 实现效果:

    相关文章

      网友评论

          本文标题:形状图层和复制图层的简单搭配使用

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