CAReplicatorLayer是一个克隆容器Layer,你添加到该Layer的内容会被克隆成instanceCount份,并且两份克隆延时instanceDelay 克隆到的位置instanceTransform等都会可控制的。
CAReplicatorLayer有以下属性:
@interface CAReplicatorLayer : CALayer
@property NSInteger instanceCount;//克隆份数,默认一份
@property BOOL preservesDepth;
@property CFTimeInterval instanceDelay;//每份
@property CATransform3D instanceTransform;//CATransform3D类型,可以是位移,旋转,缩放
@property(nullable) CGColorRef instanceColor;
@property float instanceRedOffset;
@property float instanceGreenOffset;
@property float instanceBlueOffset;
@property float instanceAlphaOffset;
@end
通过几个demo可以理解这几个属性的使用方式
示例一
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer new];
replicatorLayer.bounds = CGRectMake(0, 0, 200, 100);
replicatorLayer.backgroundColor = [UIColor grayColor].CGColor;
replicatorLayer.position = CGPointMake(self.view.center.x, self.view.center.y - 150);
[self.view.layer addSublayer:replicatorLayer];
CALayer *subLayer = [CALayer new];
subLayer.bounds = CGRectMake(0, 0, 8, 40);
subLayer.cornerRadius = 2.0f;
subLayer.position = CGPointMake(10, 115);
subLayer.backgroundColor = [UIColor redColor].CGColor;
[replicatorLayer addSublayer:subLayer];
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
basicAnimation.fromValue = @(subLayer.position.y);
basicAnimation.toValue = @(subLayer.position.y-30);
basicAnimation.repeatCount = INFINITY;
basicAnimation.autoreverses = YES;
basicAnimation.duration = 0.5;
[subLayer addAnimation:basicAnimation forKey:@"position.y"];
replicatorLayer.instanceCount = 9;//复制9份
////设置每份的距离,注意第K份是以第k-1份为参考的
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(20.0, 0.0, 0.0);
//两份之间的延时
replicatorLayer.instanceDelay = 0.5/9
// replicatorLayer.instanceColor = [UIColor greenColor].CGColor;
replicatorLayer.masksToBounds = YES;//超出边界的剪裁掉
运行效果:
示例二
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer new];
replicatorLayer.bounds = CGRectMake(0, 0, 200, 200);
replicatorLayer.backgroundColor = [UIColor grayColor].CGColor;
replicatorLayer.position = CGPointMake(self.view.center.x, self.view.center.y + 10);
[self.view.layer addSublayer:replicatorLayer];
CALayer *subLayer = [CALayer new];
subLayer.bounds = CGRectMake(0, 0, 14, 14);
subLayer.position = CGPointMake(100, 40);
subLayer.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
subLayer.borderWidth = 1.0;
subLayer.cornerRadius = 2.0;
[replicatorLayer addSublayer:subLayer];
CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scale.fromValue = @1.0;
scale.toValue = @0.1;
scale.duration = 1.5;
scale.repeatCount = INFINITY;
// scale.autoreverses = YES;
[subLayer addAnimation:scale forKey:nil];
replicatorLayer.instanceCount = 15;
CGFloat angle = (2 * M_PI)/15;
replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1.0);
//两份之间延时:scale.duration/replicatorLayer.instanceCount = 15
replicatorLayer.instanceDelay = 1.5/15;
subLayer.transform = CATransform3DMakeScale(0.1, 0.1, 0.1);
运行效果:
示例三
- (void)followAnimation
{
//
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer new];
replicatorLayer.bounds = self.view.bounds;
replicatorLayer.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75].CGColor;
replicatorLayer.position = self.view.center;
[self.view.layer addSublayer:replicatorLayer];
CALayer *subLayer = [CALayer new];
subLayer.bounds = CGRectMake(0, 0, 10, 10);
subLayer.backgroundColor =[UIColor redColor].CGColor; //[UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
subLayer.borderColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
subLayer.borderWidth = 1.0;
subLayer.cornerRadius = 5.0;
subLayer.shouldRasterize = YES;
subLayer.rasterizationScale = [UIScreen mainScreen].scale;
[replicatorLayer addSublayer:subLayer];
CAKeyframeAnimation *move = [CAKeyframeAnimation animationWithKeyPath:@"position"];
move.path = [self path];
move.repeatCount = INFINITY;
move.duration = 4.0;
// move.autoreverses = YES;
[subLayer addAnimation:move forKey:nil];
//
replicatorLayer.instanceDelay = 0.1;
replicatorLayer.instanceCount = 40;
replicatorLayer.instanceColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:1.0].CGColor;
replicatorLayer.instanceGreenOffset = -0.03;//
}
- (CGPathRef)path
{
UIBezierPath *bezierPath = [UIBezierPath new];
[bezierPath moveToPoint:(CGPointMake(31.5, 78.5))];
[bezierPath addLineToPoint:(CGPointMake(31.5, 23.5))];
[bezierPath addCurveToPoint:CGPointMake(58.5, 38.5) controlPoint1:CGPointMake(31.5, 23.5) controlPoint2:CGPointMake(62.46, 18.69)];
[bezierPath addCurveToPoint:CGPointMake(53.5, 45.5) controlPoint1:CGPointMake(57.5, 43.5) controlPoint2:CGPointMake(53.5, 45.5)];
[bezierPath addLineToPoint:(CGPointMake(43.5, 48.5))];
[bezierPath addLineToPoint:(CGPointMake(53.5, 66.5))];
[bezierPath addLineToPoint:(CGPointMake(62.5, 51.5))];
[bezierPath addLineToPoint:(CGPointMake(70.5, 66.5))];
[bezierPath addLineToPoint:(CGPointMake( 86.5, 23.5))];
[bezierPath addLineToPoint:(CGPointMake(86.5, 78.5))];
// [bezierPath addLineToPoint:(CGPointMake(31.5, 78.5))];
// [bezierPath addLineToPoint:(CGPointMake(31.5, 71.5))];
[bezierPath closePath];
CGAffineTransform T = CGAffineTransformMakeScale(3.0, 3.0);
return CGPathCreateCopyByTransformingPath(bezierPath.CGPath, &T);
}
运行效果: