『ios』动画篇CALayer-CAReplicatorLayer (波纹,loading,旋转)

最近在研究动画,发现通过CAReplicatorLayer 可以生成好多炫酷的动画。
首先我们来看下里面Api主要用到的一些api

@property NSInteger instanceCount; //图层复制的次数
@property BOOL preservesDepth;//如果设置为 YES,图层将保持和 CATransformLayer 相同的性质和限制
@property CFTimeInterval instanceDelay;//每个复制图层延迟出现
@property CATransform3D instanceTransform;//每个图层怎么移动
@property(nullable) CGColorRef instanceColor;//复制出图层的颜色

看完上面,其实就可以开始搞了。
第一个波纹动画


QQ20180701-111410-HD.gif
    CAShapeLayer *shapelayer = [CAShapeLayer layer];
    shapelayer.frame = CGRectMake(0, 0, 150, 150);
    shapelayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 150, 150)].CGPath;
    shapelayer.fillColor = [UIColor blueColor].CGColor;
    shapelayer.opacity = 0.0;
// 动画组
    //alpha
    CABasicAnimation *alpha = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alpha.fromValue = @(1.0);
    alpha.toValue = @(0.0);
    //scale
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D identidy = CATransform3DIdentity;
    scale.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(identidy, 0.0, 0.0, 0.0)];
    scale.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(identidy, 1.0, 1.0, 0.0)];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[alpha,scale];
    group.repeatCount = HUGE;
    group.autoreverses = NO;
    group.duration = 4.0;
    
    [shapelayer addAnimation:group forKey:@"animationGroup"];
    
    CAReplicatorLayer *Rplayer = [CAReplicatorLayer layer];  //复制功能
    Rplayer.instanceCount = 9.0;//数量
    Rplayer.instanceDelay = 0.6;//延迟时间
    Rplayer.frame = CGRectMake(0, 0, 150, 150);
    [Rplayer addSublayer:shapelayer];
    
//    [self.view.layer addSublayer:layer];
    
    UIView * subView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    subView.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
//    subView.backgroundColor = [UIColor redColor];
     [subView.layer addSublayer:Rplayer];
    [self.view addSubview:subView];

第二个 球的波纹动画


QQ20180701-111544-HD.gif
 CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 50, 50);
    layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)].CGPath;
    layer.fillColor = [UIColor blueColor].CGColor;
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
    
    scale.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0)];
    scale.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.2, 0.2, 0)];
    scale.autoreverses = YES;
    scale.duration = 0.5;
    scale.repeatCount = HUGE;
    [layer addAnimation:scale forKey:@"scaleAnimation"];
    
    CAReplicatorLayer *Rlayer = [CAReplicatorLayer layer];
    Rlayer.instanceDelay = 0.2;
    Rlayer.instanceCount = 4;
    Rlayer.instanceTransform = CATransform3DMakeTranslation(50, 0, 0); //每复制一个块向右移动
    Rlayer.frame = CGRectMake(100, 100, 50, 50);
    [Rlayer addSublayer:layer];
    
    UIView * subView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    subView.center = CGPointMake(100, SCREEN_HEIGHT/2);
    //    subView.backgroundColor = [UIColor redColor];
    [subView.layer addSublayer:Rlayer];
    [self.view addSubview:subView];
    

第三个 翻转

QQ20180701-111711-HD.gif

 CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(100,100, 50, 50);
    layer.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 50, 50)].CGPath;
    layer.fillColor = [UIColor blueColor].CGColor;
    
    CABasicAnimation *roteAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; //围着x轴转
    roteAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(CATransform3DIdentity, 0, 1, 0, 0)];
    roteAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(CATransform3DIdentity, M_PI,  1, 0, 0)];
    roteAnimation.duration = 1.0;
    roteAnimation.repeatCount = HUGE;
//    roteAnimation.autoreverses = YES;
    
    [layer addAnimation:roteAnimation forKey:@"rote"];
    
    CAReplicatorLayer *Rllayer = [CAReplicatorLayer layer];
    Rllayer.instanceDelay = 1;
    Rllayer.instanceCount = 8;
    Rllayer.instanceTransform = CATransform3DTranslate(CATransform3DIdentity, 0, 60, 0);
    [Rllayer addSublayer:layer];
    
    [self.view.layer addSublayer:Rllayer];
    

第四个

QQ20180701-111825-HD.gif

    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 25, 25)].CGPath;
    layer.fillColor = [UIColor blueColor].CGColor;
    layer.frame = CGRectMake(0, 0, 25, 25);
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D identity  = CATransform3DIdentity;
    
    CATransform3D fromValue = CATransform3DRotate(identity, 0.0, 0.0, 0.0, 0.0);
    scale.fromValue = [NSValue valueWithCATransform3D:fromValue];
    
    CATransform3D toValue = CATransform3DTranslate(identity, 75, 0.0, 0.0);
    toValue = CATransform3DRotate(toValue,M_PI, 0.0, 0.0, 1.0);
    
    scale.toValue = [NSValue valueWithCATransform3D:toValue];
    scale.autoreverses = NO;

    scale.repeatCount = HUGE;
    scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scale.duration = 2;
    
    
    [layer addAnimation:scale forKey:@"scaleAnimation"];
    
    CAReplicatorLayer *Rlayer = [CAReplicatorLayer layer];
    Rlayer.instanceCount = 6;
    Rlayer.instanceDelay = 0;
    Rlayer.frame = CGRectMake(0, 0, 25, 25);
    CATransform3D trans3d = CATransform3DIdentity;
    trans3d = CATransform3DTranslate(trans3d, 75, 0, 0);
    trans3d = CATransform3DRotate(trans3d, 60*M_PI/180, 0, 0, 1.0);
    
    Rlayer.instanceTransform = trans3d;
    [Rlayer addSublayer:layer];
    
    UIView * subView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 300, 300)];
    subView.center = CGPointMake(200,250);
    //    subView.backgroundColor = [UIColor redColor];
    [subView.layer addSublayer:Rlayer];
    [self.view addSubview:subView];

第五个


QQ20180701-112104-HD.gif
  CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = CGRectMake(100, 100, 8, 8);
    //闪动点
    CALayer *dot = [CALayer layer];
    dot.bounds = CGRectMake(0, 0, 8, 8);
    dot.cornerRadius = 4;
    dot.masksToBounds = YES;
    dot.position = CGPointMake(60,10);
    dot.backgroundColor = [UIColor blueColor].CGColor;
    [replicatorLayer addSublayer:dot];
    
    
    NSInteger count = 12;
    replicatorLayer.instanceCount = count;
    CGFloat angel  = 2* M_PI/count;
    replicatorLayer.instanceTransform = CATransform3DMakeRotation(angel, 0, 0, 1);
    //添加动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    CFTimeInterval duration = 1.5;
    animation.duration = duration;
    animation.fromValue = @1.0;
    animation.toValue = @0.1;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [dot addAnimation:animation forKey:nil];
    replicatorLayer.instanceDelay = duration/ count;
    dot.transform = CATransform3DMakeScale(0.01, 0.01, 0.01);
    
    UIView * subView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    subView.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    //    subView.backgroundColor = [UIColor redColor];
    [subView.layer addSublayer:replicatorLayer];
   
    [self.view addSubview:subView];

下面是demo地址。
https://github.com/Butteryflyyer/AnimateGroup 给颗星哦

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

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • 在狼群里长大的狼孩,不会觉得自己是个人,不会习惯用两条腿走路,不会喜欢用筷子吃饭。 我高中时内向文静,下课不爱乱跑...
    风约啊阅读 126评论 0 1
  • 把悲伤留在身后 对这世界敞开笑容 把眼泪藏在背后 对懦弱冷嘲热讽 永远把微笑留给别人 却不知道自己 只剩下痛苦了 ...
    雪际陸痕阅读 307评论 0 2
  • 元认知能力就是对自己思考过程的认知与理解。实质性就是对自己认知的一个加工过程,完成自我觉察、自我反省、自我评价、与...
    簡簡單單非非想阅读 191评论 0 1
  • 从创建带上架app流程 //www.greatytc.com/p/a01988025827 http://...
    ZD_0216阅读 135评论 0 0