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


// 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

  • 实现效果:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 窗外稀里哗啦地响,北风携卷着细碎的雪花,整整刮了一夜。 五点多钟,许阿姨利落地从被窝里起身,简单洗漱后,来到厨房做...
    俞壹阅读 288评论 5 8
  • 周六上午约了一个跑步的培训,打算参加完培训,就找个咖啡馆看看书学学习。背上书包,书包里面放了电脑和书籍。集合的地点...
    小花Ivan阅读 970评论 0 1
  • 1.把车贷还了 2.晚上遛狗时跑步 3.读书30-50本 4.尽量找对象 5.自学英文,西语 6.减肥15kg
    我真的要当作家阅读 424评论 1 50
  • 乐吃瘦法,目前在减肥领域最先进的一种减肥方法,也是最健康、最轻松的减肥方法! 乐吃瘦法,和传统的方法不同的是杜绝了...
    热门小东东阅读 449评论 0 0