CALayer-CAReplicatorLayer(复制图层)

** CAReplicatorLayer可以将自己的子图层复制指定的次数,并且复制体会保持被复制图层的各种基础属性以及动画 **

基本属性

  • instanceCount
    var instanceCount: Int
    拷贝图层的次数,包括其所有的子图层,默认值是1,也就是没有任何子图层被复制
  • instanceDelay
    var instanceDelay: CFTimeInterval
    在短时间内的复制延时,一般用在动画上(支持动画的延时)
  • instanceTransform
    var instanceTransform: CATransform3D
    复制图层在被创建时产生的和上一个复制图层的位移(位移的锚点时CAReplicatorlayer的中心点)
  • preservesDepth
    var preservesDepth: Bool
    如果设置为YES,图层将保持于CATransformLayer类似的性质和相同的限制

  • instanceColor
    var instanceColor: CGColor?
    设置多个复制图层的颜色,默认位白色
  • instanceRedOffset
    var instanceRedOffset: Float
    设置每个复制图层相对上一个复制图层的红色偏移量
  • instanceGreenOffset
    var instanceGreenOffset: Float
    设置每个复制图层相对上一个复制图层的绿色偏移量
  • instanceBlueOffset
    var instanceBlueOffset: Float
    设置每个复制图层相对上一个复制图层的蓝色偏移量
  • instanceAlphaOffset
    var instanceAlphaOffset: Float
    设置每个复制图层相对上一个复制图层的透明度偏移量

实例

首先我们来实现一个类似于雷达的动画,想必大家都见过这样的动画,其实使用复制图层实现起来特别的简单,这是一个比较简单的Demo,大家可以通过给图层设置图片,或者使用上一篇我们提到的CAGradientLayer来实现更加炫酷的动画

CAReplicatorLayer.gif
//
//  ViewController.swift
//  CAReplicatorLayer
//
//  Created by 蔡士林 on 6/17/16.
//  Copyright © 2016 BZ. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
        
    var replicatorLayer:CAReplicatorLayer!
    let kWidth = UIScreen.mainScreen().bounds.size.width

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        let animationView = UIView()     // 创建一个背景视图
        animationView.bounds = CGRectMake(0, 0, kWidth, 200)
        animationView.center = self.view.center
        self.view.addSubview(animationView)
        animationView.backgroundColor = UIColor.lightGrayColor()
        animationView.clipsToBounds = true
        
        let animationLayer = CAShapeLayer()       动画图层,就是不停变大的那个圆
        animationLayer.backgroundColor = UIColor.redColor().CGColor
        animationLayer.bounds = CGRectMake(0, 0, 20, 20)
        animationLayer.cornerRadius = 10
        animationLayer.position = CGPointMake(kWidth/2, 100)
        
        // 放大的动画
        let transformAnim = CABasicAnimation(keyPath: "transform")
        let value = NSValue.init(CATransform3D: CATransform3DMakeScale(10, 10, 1))
        transformAnim.toValue = value
        transformAnim.duration = 2
        
        // 透明度动画(其实也可以直接设置CAReplicatorLayer的instanceAlphaOffset来实现)
        let alphaAnim = CABasicAnimation(keyPath: "opacity")
        alphaAnim.toValue = 0
        alphaAnim.duration = 2
        
        let animGroup = CAAnimationGroup()
        animGroup.animations = [transformAnim,alphaAnim]
        animGroup.duration = 2
        animGroup.repeatCount = HUGE
        animationLayer.addAnimation(animGroup, forKey: nil)
        
        replicatorLayer = CAReplicatorLayer()
        replicatorLayer.addSublayer(animationLayer);
        replicatorLayer.instanceCount = 3  //三个复制图层
        replicatorLayer.instanceDelay = 0.3  // 复制间隔0.3秒
        animationView.layer.addSublayer(replicatorLayer)
    }
}

接下来介绍几个加载动画的用法~

CAReplicatorLayer_2.gif
//
//  ViewController.swift
//  CAReplicatorLayer
//
//  Created by 蔡士林 on 6/17/16.
//  Copyright © 2016 BZ. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var replicatorLayer:CAReplicatorLayer!
    let kWidth = UIScreen.mainScreen().bounds.size.width

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    func setupUI() {
        let animationView = UIView()
        animationView.bounds = CGRectMake(0, 0, kWidth, 300)
        animationView.center = self.view.center
        self.view.addSubview(animationView)
        animationView.backgroundColor = UIColor.lightGrayColor()
        animationView.clipsToBounds = true
        
        let animationLayer = CAShapeLayer()
        animationLayer.backgroundColor = UIColor.redColor().CGColor
        animationLayer.bounds = CGRectMake(0, 0, 20, 20)
        animationLayer.anchorPoint = CGPointMake(0.5, 0.5)
        animationLayer.position = CGPointMake(0, animationView.center.y)
        animationLayer.cornerRadius = 10

        let path = CGPathCreateMutable() // 创建转圈的动画
        CGPathAddEllipseInRect(path, nil, CGRectMake((animationView.bounds.size.width-160)/2, (animationView.bounds.size.height-160)/2, 160, 160))
        
        let transformAnim = CAKeyframeAnimation(keyPath: "position")
        transformAnim.duration = 4
        transformAnim.repeatCount = HUGE
        transformAnim.path = path
        
        animationLayer.addAnimation(transformAnim, forKey: nil)
        
        replicatorLayer = CAReplicatorLayer()
        replicatorLayer.addSublayer(animationLayer);
        replicatorLayer.repeatCount = HUGE
        replicatorLayer.instanceCount = 20
        replicatorLayer.instanceDelay = 0.2 // 动画延迟
        replicatorLayer.instanceAlphaOffset = -0.05 // 透明度递减
        animationView.layer.addSublayer(replicatorLayer)
    }
}

另一个炫酷的动画,带大小缩放的动画

CAReplicatorLayer_3.gif
//
//  ViewController.swift
//  CAReplicatorLayer
//
//  Created by 蔡士林 on 6/17/16.
//  Copyright © 2016 BZ. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var replicatorLayer:CAReplicatorLayer!
    let kWidth = UIScreen.mainScreen().bounds.size.width

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        let animationView = UIView()
        animationView.bounds = CGRectMake(0, 0, kWidth, 300)
        animationView.center = self.view.center
        self.view.addSubview(animationView)
        animationView.backgroundColor = UIColor.grayColor()
        animationView.clipsToBounds = true
        
        let animationLayer = CAShapeLayer()
        animationLayer.backgroundColor = UIColor.redColor().CGColor
        animationLayer.bounds = CGRectMake(0, 0, 20, 20)
        animationLayer.position = CGPointMake(self.view.bounds.size.width/2, 50)
        animationLayer.borderColor = UIColor.whiteColor().CGColor
        animationLayer.cornerRadius = 2
        animationLayer.borderWidth = 1
        animationLayer.transform = CATransform3DMakeScale(0.1, 0.1, 0.1)
        
        
        let transformAnim = CABasicAnimation(keyPath: "transform")
        transformAnim.duration = 2
        transformAnim.repeatCount = HUGE
        transformAnim.fromValue = NSValue.init(CATransform3D: CATransform3DMakeScale(1, 1, 1))
        transformAnim.toValue = NSValue.init(CATransform3D: CATransform3DMakeScale(0.1, 0.1, 0.1))
        
        
        
        animationLayer.addAnimation(transformAnim, forKey: nil)
        
        replicatorLayer = CAReplicatorLayer()
        replicatorLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, 300)
        replicatorLayer.addSublayer(animationLayer);
        replicatorLayer.instanceCount = 20
        replicatorLayer.instanceDelay = 0.1
        let angle = CGFloat(2*M_PI) / CGFloat(20)
        replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1.0)
        animationView.layer.addSublayer(replicatorLayer)
    }
}

我的配色比较渣渣,所以看起来不够酷,但是只要你有心,肯定是酷毙了,其实也可以通过关键帧的动画,实现书写文字啊等更加复杂的动画,以后有时间的话,会继续补充完整~
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容