QuartzCore 之 CAReplicatorLayer.h

weixin_34321977發表於2017-03-20

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"];
1880214-89b092dead85d4a2.gif
聲波

相關文章