CAReplicatorLayer
可以高效的生成许多相似的图层。它可以绘制一个或多个图层的子图层,通过设置一些属性可以在每个复制体上应用不同的变换,下边看一下例子。
Demo
本人文笔有限,可能说的不太清楚,配合源码比较好理解。
应用实例
反射图层
使用CAReplicatorLayer
并应用一个负比例变换于一个复制图层,你就可以创建指定视图(或整个视图层次)内容的镜像图片,这样就创建了一个实时的『反射』效果。
最终效果
实现
- 添加
CAReplicatorLayer
let replicatorLayer = CAReplicatorLayer.init()
// 复制图层数量,包括本身
replicatorLayer.instanceCount = 2
// alpha 的便宜
replicatorLayer.instanceAlphaOffset = -0.5
replicatorLayer.frame = CGRect(x: 20, y: 80, width: 100, height: 120)
self.view.layer.addSublayer(replicatorLayer)
- 添加图片
let image = UIImageView.init(image: UIImage.init(named: "8266801_1310119667018_1024x1024"))
image.frame = replicatorLayer.bounds
replicatorLayer.addSublayer(image.layer)
- 设置
CAReplicatorLayer
的instanceTransform
var transform = CATransform3DIdentity
transform = CATransform3DTranslate(transform, image.frame.size.width + 40, 0, 0)
transform = CATransform3DScale(transform, -1, 1, 0)
平移动画
使用CAReplicatorLayer
并设置instanstransform,你就可以创建指定视图(或整个视图层次)内容的镜像图片,并且给CAReplicatorLayer
添加animation,就可以得到平移的动画。
最终效果
- 添加
CAReplicatorLayer
和图片
let replicatorLayer = CAReplicatorLayer.init()
replicatorLayer.instanceCount = 3
replicatorLayer.frame = CGRect(x: 20, y: 230, width: 80, height: 100)
self.view.layer.addSublayer(replicatorLayer)
let image = UIImageView.init(image: UIImage.init(named: "8266801_1310119667018_1024x1024"))
image.frame = replicatorLayer.bounds
replicatorLayer.addSublayer(image.layer)
- 给
CAReplicatorLayer
添加平移动画
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(100, 0, 0)
let animation = CABasicAnimation.init(keyPath: "instanceTransform.translation.x")
animation.toValue = 130
animation.repeatCount = MAXFLOAT
animation.duration = 2
//动画结束后再返回
animation.autoreverses = true
// 在replicatorLayer添加动画时,是layer上的所有副本一起动,instanceDelay并没有用
replicatorLayer.addAnimation(animation, forKey: "")
笔迹动画
利用国外大神写的code可以将文字转换成UIBezierPath
,然后生成一个layer
,给layer
添加CAKeyframeAnimation
,让CAKeyframeAnimation
的path
设置为刚刚生成的UIBezierPath
,并设置CAKeyframeAnimation
的属性即可。
最终效果
- 添加文字的
UIBezierPath
// 具体生成请看demo
let path = bezierPathFrom("chatwyn")
textPath.frame = CGPathGetBoundingBox(path.CGPath)
textPath.position = view.center
textPath.geometryFlipped = true
textPath.path = path.CGPath
textPath.fillColor = nil
textPath.lineWidth = 1
textPath.strokeColor = nil
self.view.layer.addSublayer(textPath)
- 添加
CAReplicatorLayer
和副本layer
let replicatorLayer = CAReplicatorLayer.init()
replicatorLayer.frame = textPath.bounds
textPath.addSublayer(replicatorLayer)
let layer = CALayer.init()
layer.frame = CGRectMake(34, 5,2, 2)
layer.cornerRadius = 1
layer.backgroundColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 1).CGColor
replicatorLayer.addSublayer(layer)
- 给
layer
添加CAKeyframeAnimation
replicatorLayer.instanceDelay = duration / CFTimeInterval(replicatorCount)
replicatorLayer.instanceCount = replicatorCount
replicatorLayer.instanceRedOffset = -0.005
replicatorLayer.instanceGreenOffset = -0.003
replicatorLayer.instanceBlueOffset = -0.001
let positionAnmation = CAKeyframeAnimation.init(keyPath: "position")
positionAnmation.path = textPath.path
positionAnmation.duration = duration
positionAnmation.repeatCount = MAXFLOAT
// 需要添加到被复制的layer上才会有delay
replicatorView.addAnimation(positionAnmation, forKey: "")
End
- 只要你脑洞大开,通过设置
instanceTransform
还有很多其他炫彩的效果,本人脑洞是不够啊,逃~~ - 如有问题或建议请联系我,博客地址甩出来http://chatwyn.ml/