CAReplicatorLayer是一个layer容器,会对其中的subLayer进行复制和属性偏移,通过它,可以创建出类似倒影的效果,也可以进行变换复制。Apple 提供的 API如下所示:
#import <QuartzCore/CALayer.h>
NS_ASSUME_NONNULL_BEGIN
CA_CLASS_AVAILABLE (10.6, 3.0, 9.0, 2.0)
@interface CAReplicatorLayer : CALayer
//subLayer 复制的次数
@property NSInteger instanceCount;
//如果设置为YES,图层将保持于CATransformLayer类似的性质和相同的限制
@property BOOL preservesDepth;
//如果进行动画,副本延时一秒执行
@property CFTimeInterval instanceDelay;
//每个副本在 x , y , z 轴方向的改变
@property CATransform3D instanceTransform;
//副本颜色
@property(nullable) CGColorRef instanceColor;
//设置每个复制图层相对上一个复制图层的红色偏移量
@property float instanceRedOffset;
//设置每个复制图层相对上一个复制图层的绿色偏移量
@property float instanceGreenOffset;
//设置每个复制图层相对上一个复制图层的蓝色偏移量
@property float instanceBlueOffset;
//设置每个复制图层相对上一个复制图层的透明度偏移量
@property float instanceAlphaOffset;
@end
NS_ASSUME_NONNULL_END
实例一:(雷达波纹)
UIView *animationView = [[UIView alloc] init];
animationView.bounds = CGRectMake(0, 0, kScreenWidth, 200);
animationView.center = self.view.center;
animationView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:animationView];
CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.instanceCount = 3;
replicatorLayer.instanceDelay = 0.3;
[animationView.layer addSublayer:replicatorLayer];
//动画图层,就是不停变大的那个圆
animationLayer = [CAShapeLayer layer];
animationLayer.backgroundColor = [UIColor lightGrayColor].CGColor;
animationLayer.bounds = CGRectMake(0, 0, 20, 20);
animationLayer.cornerRadius = 20/2.0;
animationLayer.position = CGPointMake(kScreenWidth/2.0, 200/2.0);
animationLayer.opacity = 0.7;
[replicatorLayer addSublayer:animationLayer]; //把 动画图层(shaperLayer)关联到 复制图层(replicatorLayer)上
//放大动画
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeScale(10, 10, 0.5)];
transformAnimation.toValue = value;
transformAnimation.duration = 2.0;
//透明度动画 (也可以直接设置CAReplicatorLayer的instanceAlphaOffset来实现,学习中,目前实现效果不佳,请大神指教)
CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.fromValue = @(1.0);
alphaAnimation.toValue = @(0.0);
alphaAnimation.duration = 2.0;
CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0.0];
opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
//动画组
animationGroup = [CAAnimationGroup animation];
animationGroup.animations = @[transformAnimation,alphaAnimation];
animationGroup.duration = 2.0;
animationGroup.repeatCount = MAXFLOAT;
//开始动画
[animationLayer addAnimation:animationGroup forKey:@"animationLayer"];
声波
网友评论