CAReplicatorLayer

效果一

Snip20170821_2.png
- (void)replicatorBase{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 200);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];
    
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(50, 50, 50, 50);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.cornerRadius = 10;
    layer.masksToBounds = YES;
    
    CGFloat replicatorWidth = 50;
    CGFloat replicatorHeight = 50;
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.position = CGPointMake(containView.frame.size.width/2, containView.frame.size.height/2);
    replicatorLayer.bounds = CGRectMake(0, 0, replicatorWidth, replicatorHeight);
    replicatorLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [containView.layer addSublayer:replicatorLayer];
    
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(transform, M_PI / 5.0, 0, 0, 1);
    replicatorLayer.instanceTransform = transform;
    replicatorLayer.instanceCount = 20;
    replicatorLayer.instanceDelay = 0.2;
    replicatorLayer.repeatCount = HUGE;
        replicatorLayer.instanceBlueOffset = -0.05;
        replicatorLayer.instanceGreenOffset = -0.05;
    [replicatorLayer addSublayer:layer];
    //replictorLayer 和其上面的layer的关系:replicatorlayer负责创建多个重复的容器,用于存放layer,另一方便负责容器的摆放位置(位移,缩放,旋转),layer在replicationlayer的位置共同决定了最终layer的位置。
}

效果二

Snip20170821_3.png
- (void)replicatorCircleBase{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 300);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];
    
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, 20, 20);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.cornerRadius = 10;
    layer.masksToBounds = YES;
    
    CGFloat replicatorWidth = 50;
    CGFloat replicatorHeight = 100;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, nil,CGRectMake((replicatorWidth - CGRectGetWidth(containView.frame))/2, (replicatorHeight - CGRectGetHeight(containView.frame))/2, CGRectGetWidth(containView.frame), CGRectGetHeight(containView.frame)));
    //圆周运动和containview和replicatorlayer两者都有关系,具体原因无法确定。
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = 4;
    animation.repeatCount = HUGE;
    animation.path = path;
    [layer addAnimation:animation forKey:nil];
    
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.position = CGPointMake(containView.frame.size.width/2, containView.frame.size.height/2);
    replicatorLayer.bounds = CGRectMake(0, 0, replicatorWidth, replicatorHeight);
    replicatorLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [containView.layer addSublayer:replicatorLayer];
    
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(transform, M_PI / 5.0, 0, 0, 1);
    //        replicatorLayer.instanceTransform = transform;
    replicatorLayer.instanceCount = 20;
    replicatorLayer.instanceDelay = 0.2;
    replicatorLayer.repeatCount = HUGE;
    //    replicatorLayer.instanceBlueOffset = -0.05;
    //    replicatorLayer.instanceGreenOffset = -0.05;
    [replicatorLayer addSublayer:layer];
}

效果三

Snip20170821_4.png
- (void)replicatorCircleHigh{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 200);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];
    
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(50, 50, 20, 20);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.cornerRadius = 10;
    layer.masksToBounds = YES;
    layer.transform = CATransform3DMakeScale(0.1, 0.1, 0.1);
    
    CGFloat replicatorWidth = 50;
    CGFloat replicatorHeight = 50;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.duration = 2;
    animation.repeatCount = HUGE;
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 0.1)];
    [layer addAnimation:animation forKey:nil];
    
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.position = CGPointMake(containView.frame.size.width/2, containView.frame.size.height/2);
    replicatorLayer.bounds = CGRectMake(0, 0, replicatorWidth, replicatorHeight);
    replicatorLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [containView.layer addSublayer:replicatorLayer];
    
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(transform, 2*M_PI/20.0, 0, 0, 1);
    // M_2_PI =2/π
    replicatorLayer.instanceTransform = transform;
    replicatorLayer.instanceCount = 20;
    replicatorLayer.instanceDelay = 0.1;
    replicatorLayer.repeatCount = HUGE;
    //    replicatorLayer.instanceBlueOffset = -0.05;
    //    replicatorLayer.instanceGreenOffset = -0.05;
    [replicatorLayer addSublayer:layer];

}

效果四

Snip20170821_5.png
- (void)replicatorCircleHigh2{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 200);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];
    
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(50, 50, 10, 30);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    
    CGFloat replicatorWidth = 50;
    CGFloat replicatorHeight = 50;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.duration = 2;
    animation.repeatCount = HUGE;
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 0.1)];
//    [layer addAnimation:animation forKey:nil];
    
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.position = CGPointMake(containView.frame.size.width/2, containView.frame.size.height/2);
    replicatorLayer.bounds = CGRectMake(0, 0, replicatorWidth, replicatorHeight);
    replicatorLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [containView.layer addSublayer:replicatorLayer];
    
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(transform, 2*M_PI/20.0, 0, 0, 1);
    replicatorLayer.instanceTransform = transform;
    replicatorLayer.instanceCount = 20;
    replicatorLayer.instanceDelay = 0.1;
    replicatorLayer.repeatCount = HUGE;
    [replicatorLayer addSublayer:layer];

}

效果五

Snip20170821_1.png
- (void)indictor{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 200);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];
    
    CGFloat layerWidth = 10.0;
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(CGRectGetMidX(containView.bounds), 0, layerWidth, layerWidth*2.5);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.opacity = 0.f;
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    basicAnimation.fromValue = @1;
    basicAnimation.toValue = @0;
    basicAnimation.duration = 1.f;
    basicAnimation.repeatCount = HUGE;
    [layer addAnimation:basicAnimation forKey:nil];
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = containView.bounds;
    replicatorLayer.instanceCount = 30.f;
    replicatorLayer.instanceDelay = 1/30.f;
    replicatorLayer.instanceTransform = CATransform3DMakeRotation(2*M_PI/30, 0, 0, 1);
    [containView.layer addSublayer:replicatorLayer];
    [replicatorLayer addSublayer:layer];
}

效果六

Snip20170821_6.png
- (void)waveAnimation{
    UIView *view = [[UIView alloc]init];
    view.center = self.view.center;
    view.bounds = CGRectMake(0, 0, self.view.frame.size.width, 300);
    view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view];
    
    CGFloat width = 15.f;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.bounds = CGRectMake(0, 0, width, width);
    shapeLayer.position = CGPointMake(self.view.bounds.size.width/2, 50);
    shapeLayer.backgroundColor = [UIColor greenColor].CGColor;
    shapeLayer.cornerRadius = width/2;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D scaleTransform = CATransform3DMakeScale(100, 100, 100);
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:scaleTransform];
    animation.duration = 2.f;
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.toValue = @(0);
    opacityAnimation.duration = 2.f;
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[animation,opacityAnimation];
    group.duration = 2.f;
    group.repeatCount = CGFLOAT_MAX;
    [shapeLayer addAnimation:group forKey:nil];
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    [replicatorLayer addSublayer:shapeLayer];
    replicatorLayer.instanceCount = 4;
    replicatorLayer.instanceDelay = 0.5f;
    [view.layer addSublayer:replicatorLayer];
}

效果七

Snip20170821_7.png
- (void)threePointScale{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 200);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];
    
    CGFloat interval = 5;
    CGFloat radius = (100 - 2*interval)/3;
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, (100 - radius)/2, radius, radius);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.cornerRadius = radius/2;
    layer.masksToBounds = YES;
    
    CGFloat replicatorWidth = 100;
    CGFloat replicatorHeight = 100;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.9;
    animation.repeatCount = HUGE;
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 0)];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.2, 0.2, 0)];
    [layer addAnimation:animation forKey:nil];
    
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.position = CGPointMake(containView.frame.size.width/2, containView.frame.size.height/2);
    replicatorLayer.bounds = CGRectMake(0, 0, replicatorWidth, replicatorHeight);
    replicatorLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [containView.layer addSublayer:replicatorLayer];
    
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DMakeTranslation(radius + interval, 0, 0);
    replicatorLayer.instanceTransform = transform;
    replicatorLayer.instanceCount = 3;
    replicatorLayer.instanceDelay = 0.2;
    replicatorLayer.repeatCount = HUGE;
    [replicatorLayer addSublayer:layer];
}

效果八

Snip20170821_8.png
- (void)replicatorLayerTriangle{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 200);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];
    
    CGFloat radius = 100/4;
    CGFloat transX = 100 - radius;
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, radius, radius);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.cornerRadius = radius/2;
    layer.masksToBounds = YES;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.duration = 1;
    animation.repeatCount = HUGE;
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(CATransform3DIdentity, 0, 0, 0, 0)];
    CATransform3D toValue = CATransform3DTranslate(CATransform3DIdentity, transX, 0.0, 0.0);
    toValue = CATransform3DRotate(toValue,120.0*M_PI/180.0, 0.0, 0.0, 1.0);
    animation.toValue = [NSValue valueWithCATransform3D:toValue];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [layer addAnimation:animation forKey:nil];
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];

    CGFloat ceterx = containView.frame.size.width/2 - transX/2;
    //等边三角形理论支撑
    CGFloat cetery = containView.frame.size.height/2 - (1/3.0)*(transX/2)*sqrtf(3.0);
    replicatorLayer.position = CGPointMake(ceterx, cetery);
    replicatorLayer.bounds = CGRectMake(0, 0, radius, radius);
    replicatorLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [containView.layer addSublayer:replicatorLayer];
    
    CATransform3D transform = CATransform3DIdentity;
    //这里有个技巧,第二个点和第一个点平行又和第一个点成120度夹角。否则动画效果看起来特别别扭。如果是四个点也是同样的道理
    transform = CATransform3DTranslate(transform, transX, 0, 0);
    transform = CATransform3DRotate(transform, 120.0*M_PI/180.0, 0, 0, 1);
    replicatorLayer.instanceTransform = transform;
    replicatorLayer.instanceCount = 3;
    replicatorLayer.instanceDelay = 0.0;
    replicatorLayer.repeatCount = HUGE;
    [replicatorLayer addSublayer:layer];
}

效果九

Snip20170821_9.png
- (void)replicatorLayerManyPoint{
    UIView *containView = [[UIView alloc]init];
    containView.center = self.view.center;
    containView.bounds = CGRectMake(0, 0, 200, 200);
    containView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containView];

    NSInteger column = 3;
    CGFloat between = 5.0;
    CGFloat radius = (100 - between * (column - 1))/column;
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, radius, radius);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.cornerRadius = radius/2;
    layer.masksToBounds = YES;
    
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnimation.fromValue = @(1.0);
    alphaAnimation.toValue = @(0.0);
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 0)];
    scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.2, 0.2, 0)];
    scaleAnimation.repeatCount = HUGE;
    scaleAnimation.duration = 0.6;
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[alphaAnimation,scaleAnimation];
    group.repeatCount = HUGE;
    group.duration = 1.f;
    [layer addAnimation:group forKey:nil];

    CAReplicatorLayer *replicatorLayerX = [CAReplicatorLayer layer];
    replicatorLayerX.backgroundColor = [UIColor yellowColor].CGColor;
    replicatorLayerX.frame = CGRectMake(0, 0, 100, radius);
    replicatorLayerX.instanceDelay = 0.3;
    replicatorLayerX.instanceCount = 3;
    replicatorLayerX.instanceTransform = CATransform3DTranslate(CATransform3DIdentity, radius+between, 0, 0);
    [replicatorLayerX addSublayer:layer];
    
    CAReplicatorLayer *replicatorLayerY = [CAReplicatorLayer layer];
    replicatorLayerY.backgroundColor = [UIColor cyanColor].CGColor;
    replicatorLayerY.position = CGPointMake(containView.frame.size.width/2, containView.frame.size.height/2);
    replicatorLayerY.bounds = CGRectMake(0, 0, 100, 100);
    replicatorLayerY.instanceDelay = 0.3;
    replicatorLayerY.instanceCount = 3;
    replicatorLayerY.instanceTransform = CATransform3DTranslate(CATransform3DIdentity, 0, radius+between, 0);
    [replicatorLayerY addSublayer:replicatorLayerX];
    [containView.layer addSublayer:replicatorLayerY];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 文/橘笙之南 爱上一个人,就是你开始学会了投降,向那个你爱上的人缴械投降,无论你从前是个多么倔强不羁的,多么桀骜不...
    橘笙之南阅读 359评论 0 3
  • 嘟嘟妈亲子阅读打卡第295-296天 这两天晚上还是剪纸,剪纸,剪纸。剪猫头鹰,剪狮子,乱剪一通。 英语启蒙:多纳...
    霍嘟嘟妈妈阅读 223评论 0 0
  • 现在 UI培训这边的培训告一段落,简历也都按照自己的想法做好了,打算周一的时候开始投简历。关于工作,目前的目标比较...
    方博儿阅读 417评论 0 0
  • 为让学生更加健康成长,促进师生、家长和教师之间,学校和家庭之间有效的沟通交流,共同教育好孩子,本学期,根据上级有关...
    高可雨阅读 480评论 0 0