iOS开发 专用图层(四)CAReplicatorLayer

本文介绍如何利用CAReplicatorLayer在iOS应用中生成动画效果,包括生成正方形围成的圈圈和制作倒影效果的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    CAReplicatorLayer 的主要作用是生成相似的图层。

    先来一个生成一串正方形围成的圈圈的图片,代码比较简单,如下:

    

//
//  CAReplicatorLayerViewController.m
//  iOSanimation
//
//  Created by biyabi on 15/12/1.
//  Copyright © 2015年 caijunrong. All rights reserved.
//

#import "CAReplicatorLayerViewController.h"

@interface CAReplicatorLayerViewController ()

@property (nonatomic, strong) UIView *GView;

@end

@implementation CAReplicatorLayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    
    CAReplicatorLayer *replicator = [CAReplicatorLayer layer];
    replicator.frame = self.GView.bounds;
    [self.view.layer addSublayer:replicator];
    
    replicator.instanceCount = 10;
    
    //旋转方向和角度主要还是由自己决定
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, 0, 100, 0);
    transform = CATransform3DRotate(transform, M_PI/5, 0, 0, 1);
    
    transform = CATransform3DTranslate(transform, 0, -100, 0);
    
    //
    replicator.instanceTransform = transform;

    replicator.instanceBlueOffset = -0.1;
    replicator.instanceGreenOffset = -0.1;
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100*2, 100.0f, 100.0f, 100.0f);
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    [replicator addSublayer:layer];
}


- (UIView *)GView{
    if (!_GView) {
        _GView  = [[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 10*2, 94, 10,10)];
        [self.view addSubview:_GView];
    }
    return _GView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


    倒影更简单了:

//
//  ReView.m
//  iOSanimation
//
//  Created by biyabi on 15/12/1.
//  Copyright © 2015年 caijunrong. All rights reserved.
//

#import "ReView.h"

@implementation ReView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [ super initWithFrame:frame]) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
        [self addSubview:imageView];
        [imageView setImage:[UIImage imageNamed:@"icon_weibo"]];
        
        //做倒影
        CAReplicatorLayer *layer = (CAReplicatorLayer *)self.layer;
        layer.instanceCount = 2;
        
        CATransform3D transform = CATransform3DIdentity;
        CGFloat verticalOffset = frame.size.height + 2;
        transform = CATransform3DTranslate(transform, 0, verticalOffset, 0);
        transform = CATransform3DRotate(transform, M_PI, 1, 0, 0);
//        transform = CATransform3DScale(transform, 1, 0 , 0);
        layer.instanceTransform = transform;
    
        //reduce alpha of reflection layer
        layer.instanceAlphaOffset = -0.6;
        
        
        
    }
    return self;
}

+ (Class)layerClass
{
    return [CAReplicatorLayer class];
}

@end



转载于:https://my.oschina.net/caijunrong/blog/537885

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值