@property (nonatomic,strong) CAReplicatorLayer *replicatorLayer;
@property (nonatomic,strong) CALayer *layer;
@property (nonatomic,strong) CABasicAnimation *basicAnimation;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view.layer addSublayer:self.replicatorLayer];
[self.replicatorLayer addSublayer:self.layer];
[self.layer addAnimation:self.basicAnimation forKey:nil];
}
- (CALayer *)layer
{
if (_layer == nil) {
_layer = [CALayer layer];
_layer.backgroundColor = [UIColor greenColor].CGColor;
_layer.bounds = CGRectMake(0, 0, 30, 100);
_layer.position = CGPointMake(15, 200);
_layer.anchorPoint = CGPointMake(0.5, 1);
}
return _layer;
}
- (CAReplicatorLayer *)replicatorLayer
{
if (_replicatorLayer == nil) {
_replicatorLayer = [CAReplicatorLayer layer];
_replicatorLayer.frame = CGRectMake(50, 50, 200, 200);
_replicatorLayer.backgroundColor = [UIColor blueColor].CGColor;
// 设置4个子层,3个复制层
_replicatorLayer.instanceCount = 4;
// 设置复制子层的相对位置,每个x轴相差40
_replicatorLayer.instanceTransform = CATransform3DMakeTranslation(40, 0, 0);
// 设置复制子层的延迟动画时长
_replicatorLayer.instanceDelay = 0.3;
}
return _replicatorLayer;
}
- (CABasicAnimation *)basicAnimation
{
if (_basicAnimation == nil) {
_basicAnimation = [CABasicAnimation animation];
_basicAnimation.keyPath = @"transform.scale.y";
_basicAnimation.toValue = @(0.1);
_basicAnimation.autoreverses = YES;
_basicAnimation.repeatCount = MAXFLOAT;
}
return _basicAnimation;
}
@end