CAReplicatorLayer 一般用來復制層或者反射牵素。
3.gif
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIView *containView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.containView =[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 -100, self.view.frame.size.height/2 - 100, 200, 200)];
self.containView.backgroundColor =[UIColor redColor];
[self.view addSubview:self.containView];
CAReplicatorLayer *replayer =[CAReplicatorLayer layer];
replayer.frame = self.containView.bounds;
[self.containView.layer addSublayer:replayer];
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, replayer.bounds.size.height -150, 10, 150);
layer.backgroundColor = [UIColor whiteColor].CGColor;
[replayer addSublayer:layer];
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform.scale.y";
anim.toValue = @0.1;
anim.duration = 0.5;
anim.repeatCount = MAXFLOAT;
// 設置動畫反轉
anim.autoreverses = YES;
[layer addAnimation:anim forKey:nil];
// 復制層中子層總數
// instanceCount:表示復制層里面有多少個子層,包括原始層
replayer.instanceCount = 5;
// 設置復制子層偏移量娜谊,不包括原始層,相對于原始層x偏移
// replayer.instanceTransform = CATransform3DIdentity;
CATransform3D tranform = CATransform3DIdentity;
tranform = CATransform3DMakeTranslation(45, 0, 0);
replayer.instanceTransform =tranform;
// 設置復制層動畫延遲時間
replayer.instanceDelay = 0.1;
// 如果設置了原始層背景色蝇棉,就不需要設置這個屬性
replayer.instanceColor = [UIColor greenColor].CGColor;
replayer.instanceGreenOffset = -0.3;
}