很久之前看过一篇讲CATransform3D的文章,现在找不到了...,这个效果是文章里面的,现在重新实现一下这个效果。
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scroll = [UIScrollView new];
scroll.frame = [UIScreen mainScreen].bounds;
scroll.backgroundColor = [UIColor blackColor];
scroll.contentSize = CGSizeMake(scroll.bounds.size.width*3, scroll.bounds.size.width*3);
[self.view addSubview:scroll];
//设置父图层的sublayerTransform属性的m34值,那么所有的子图层都会生效,且共同享有一个灭点
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0 / 500;
scroll.layer.sublayerTransform = transform;
int row = 5;
int col = 5;
int level = 11;
int wight = 50;
int margin = 30;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
for (int i=0; i<row; i++) { //行
for (int j=0; j<col; j++) { //列
for (int k=0; k<level; k++) { //层
CALayer *layer = [CALayer layer];
//设置背景色,越底部的图层颜色越深
CGFloat color = 0.2+k*(0.8/level);
layer.backgroundColor = [UIColor colorWithRed:color green:color blue:color alpha:1].CGColor;
layer.opaque = YES;
layer.frame = CGRectMake(200 + i*(margin+wight), 200 + j*(margin+wight), 50, 50);
CATransform3D transform = CATransform3DMakeTranslation(0, 0, k*50-150);
//动画
CABasicAnimation *anim = [CABasicAnimation animation];
anim.fromValue = @(CATransform3DMakeTranslation(0, 0, 0));
anim.toValue = @(transform);
anim.duration = k*0.2;
anim.keyPath = @"transform";
anim.removedOnCompletion = NO; //动画完成时不移除动画
anim.fillMode = kCAFillModeForwards;
[layer addAnimation:anim forKey:@""];
[scroll.layer addSublayer:layer];
}
}
}
});
}