CAReplicatorLayer
的目的是为了高效生成许多相似的图层。它会绘制一个或多个图层的子图层,并在每个复制体上应用不同的变换。看上去演示能够更加解释这些,我们来写个例子吧。
更多CAReplicatorLayer的了解:https://zsisme.gitbooks.io/ios-/content/chapter6/careplicatorLayer.html
原文改版来自:
链接里面的文章是swift写的 在这里,我用object-c的方式还原一下实现方式
1.自定义一个view:
CAReplicatorLayerView
icatorLayer];}//上线滚动的Layer的添加动画-(void)animateLineLayer{ CABasicAnimation *moveRectangle = [CABasicAnimation animationWithKeyPath:@"position.y"];// CABasicAnimation(keyPath: "position.y") moveRectangle.toValue = @80.0; moveRectangle.duration = 0.5; moveRectangle.autoreverses = true; moveRectangle.repeatCount = HUGE; [replicatorLayer addAnimation:moveRectangle forKey:@"ss"];}@end
查看一下效果
效果图:
以下是实现代码
//
// CAReplicatorLayerView.m
// SearchVCDemo
//
// Created by Programmer two on 16/1/18.
// Copyright © 2016年 linpeng. All rights reserved.
//
#import "CAReplicatorLayerView.h"
@implementation CAReplicatorLayerView
int height = 150;
int width = 40;
int margin = 10;
int count = 3;
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
//创建mainLayer
[self addMainLayer];
//创建上线滚动的Layer
[self addLineLayer];
//上线滚动的Layer的添加动画
[self animateLineLayer];
}
return self;
}
//创建mainLayer
CAReplicatorLayer *replicatorLayerMAIN;
-(void)addMainLayer
{
replicatorLayerMAIN = [CAReplicatorLayer layer];
replicatorLayerMAIN.bounds = self.bounds;
replicatorLayerMAIN.anchorPoint = CGPointMake(0, 0);
replicatorLayerMAIN.instanceCount = 2;
[self.layer addSublayer:replicatorLayerMAIN];
//复制三个一样的 replicatorLayerMAIN 来实现
// replicatorLayerMAIN.masksToBounds = YES;
replicatorLayerMAIN.instanceCount = 3;
replicatorLayerMAIN.instanceDelay = 0.3;
replicatorLayerMAIN.instanceTransform = CATransform3DMakeTranslation(width+margin, 0, 0);
}
//创建上线滚动的Layer
CAReplicatorLayer *replicatorLayer;
-(void)addLineLayer
{
replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.bounds = CGRectMake(0,0, width, height);
replicatorLayer.backgroundColor = [UIColor whiteColor].CGColor;
replicatorLayer.cornerRadius = 2;
replicatorLayer.anchorPoint = CGPointMake(0, 0.6);
replicatorLayer.position = CGPointMake((self.frame.size.width - (count*width)-(count - 1)*margin)/2.0, 0);
[replicatorLayerMAIN addSublayer:repl<pre name="code" class="html">// 16进制转颜色
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define kMAINGREEN UIColorFromRGB(0x3CDC46)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:kMAINGREEN];
CAReplicatorLayerView *v1 = [[CAReplicatorLayerView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 320)/2.0, 300, 320, 200)];
[self.view addSubview:v1];
}
icatorLayer];}//上线滚动的Layer的添加动画-(void)animateLineLayer{ CABasicAnimation *moveRectangle = [CABasicAnimation animationWithKeyPath:@"position.y"];// CABasicAnimation(keyPath: "position.y") moveRectangle.toValue = @80.0; moveRectangle.duration = 0.5; moveRectangle.autoreverses = true; moveRectangle.repeatCount = HUGE; [replicatorLayer addAnimation:moveRectangle forKey:@"ss"];}@end
2.使用这个自定义View
// 16进制转颜色
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define kMAINGREEN UIColorFromRGB(0x3CDC46)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:kMAINGREEN];
CAReplicatorLayerView *v1 = [[CAReplicatorLayerView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 320)/2.0, 300, 320, 200)];
[self.view addSubview:v1];
}
查看一下效果
自定义View里面有这么一句:
replicatorLayerMAIN.masksToBounds =YES;
或者根据修改坐标点
replicatorLayer.anchorPoint =CGPointMake(0,0.6);
replicatorLayer.position =CGPointMake((self.frame.size.width - (count*width)-(count -1)*margin)/2.0,0);
或者修改动画
可以弄出好几种样式
关键几句:
replicatorLayerMAIN.instanceCount = 3;
replicatorLayerMAIN.instanceDelay = 0.3;
replicatorLayerMAIN.instanceTransform = CATransform3DMakeTranslation(width+margin, 0, 0);
==============================================================================================
另一种样式的使用
==============================================================================================
实现代码
//
// CAReplicatorLayerView.m
// SearchVCDemo
//
// Created by Programmer two on 16/1/18.
// Copyright © 2016年 linpeng. All rights reserved.
//
#import "CAReplicatorLayerView.h"
@implementation CAReplicatorLayerView
int height = 40;
int width = 40;
int margin = 10;
int count = 3;
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
//创建mainLayer
[self addMainLayer];
//创建上线滚动的Layer
[self addLineLayer];
//上线滚动的Layer的添加动画
[self animateLineLayer];
}
return self;
}
//创建mainLayer
CAReplicatorLayer *replicatorLayerMAIN;
-(void)addMainLayer
{
replicatorLayerMAIN = [CAReplicatorLayer layer];
replicatorLayerMAIN.bounds = CGRectMake(0,0, self.frame.size.width, self.frame.size.height);
// replicatorLayerMAIN.anchorPoint = CGPointMake(0.5, 0.5);
replicatorLayerMAIN.position = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
replicatorLayerMAIN.instanceCount = 2;
[self.layer addSublayer:replicatorLayerMAIN];
//复制三个一样的 replicatorLayerMAIN 来实现
replicatorLayerMAIN.masksToBounds = YES;
replicatorLayerMAIN.instanceCount = 15;
replicatorLayerMAIN.instanceDelay = 1/15.0;
CGFloat angle = (2.0 * M_PI) / (15.0);
replicatorLayerMAIN.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
}
//创建上线滚动的Layer
CAReplicatorLayer *replicatorLayer;
-(void)addLineLayer
{
replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.bounds = CGRectMake(0,0, width, height);
replicatorLayer.backgroundColor = [UIColor whiteColor].CGColor;
replicatorLayer.cornerRadius = width/2.0;
replicatorLayer.position = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0 - 100);
[replicatorLayerMAIN addSublayer:replicatorLayer];
}
//上线滚动的Layer的添加动画
-(void)animateLineLayer
{
CABasicAnimation *moveRectangle = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
moveRectangle.fromValue = @1;
moveRectangle.toValue = @0.1;
moveRectangle.duration = 1;
moveRectangle.repeatCount = HUGE;
[replicatorLayer addAnimation:moveRectangle forKey:@"ss"];
}
@end
效果图: