iOS CAReplicatorLayer简单笔记

CAReplicatorLayer的目的是为了高效生成许多相似的图层。它会绘制一个或多个图层的子图层,并在每个复制体上应用不同的变换。看上去演示能够更加解释这些,我们来写个例子吧。

原文改版来自:

链接里面的文章是swift写的 在这里,我用object-c的方式还原一下实现方式

1.自定义一个view:  CAReplicatorLayerView

以下是实现代码

//
//  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

效果图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值