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)
}
}

我的配色比较渣渣,所以看起来不够酷,但是只要你有心,肯定是酷毙了,其实也可以通过关键帧的动画,实现书写文字啊等更加复杂的动画,以后有时间的话,会继续补充完整~

CAReplicatorLayer 详解的更多相关文章

  1. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  2. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  3. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  4. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  5. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  6. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  7. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

  8. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  9. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

随机推荐

  1. 使 windows 无需输入开机密码自动进入系统

    步骤 运行netplwiz: 勾去图中所示复选框,确定后输入密码.

  2. C++11 function用法 可调用对象模板类

    std::function<datatype()> ()内写参数类型 datatype 代表function的返回值 灵活的用法.. 代码如下 #include <stdio.h&g ...

  3. 干净卸载 Cloudera CDH 5 beta2

    Cloudera 的官方介绍: http://www.cloudera.com/content/cloudera-content/cloudera-docs/CM4Ent/4.8.1/Cloudera ...

  4. C++容器类-vector

    vecto之简单应用: #include<vector> #include<iostream> using namespace std; int main() { vector ...

  5. 用简单的语言描述C++ 是什么?

    用简单的语言描述C++ 是什么? 答:C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛.C++支持多种编程范式 --面向对象编程.泛型编程和过程化编程. 其编程领域众广,常用于系统开发,引 ...

  6. 【0624作业】使用Scanner类输入并显示会员卡号

    package com.work0624; /** * 练习题 * 使用Scanner类输入并显示会员卡号 * @author L */ import java.util.Scanner; publi ...

  7. jfinal的配置文件详解

    1.去官网下载最新的jar包(我这是JFinal-lib-2.2) tomcat+mysql 所需要的jar 2.配置web.xml <filter> <filter-name> ...

  8. 将远程分支拷贝到本地,并更新代码push到原分支

    第一步:git clone +主分支 第二步:git fetch origin 分支名 第三步:git checkout -b 分支名 origin/分支名 第四步:git pull origin 分 ...

  9. linux常用命令(配置查看,定时任务)

    1.查看所有待挂载设备信息 fdisk -l # fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders, total sector ...

  10. 01Qt中的隐式共享

    隐式共享 ​ 隐式共享又称为回写复制(copy on write).当两个对象共享同一分数据时(通过浅拷贝实现数据共享),如果数据不改变,则不进行数据的复制.而当某个对象需要需要改变数据时,则进行深拷 ...