CAReplicatorLayer可以高效的生成許多相似的圖層。它可以繪制一個或多個圖層的子圖層,通過設置一些屬性可以在每個復制體上應用不同的變換,下邊看一下例子。
Demo
本人文筆有限,可能說的不太清楚,配合源碼比較好理解。
應用實例
反射圖層
使用CAReplicatorLayer并應用一個負比例變換于一個復制圖層,你就可以創(chuàng)建指定視圖(或整個視圖層次)內容的鏡像圖片,這樣就創(chuàng)建了一個實時的『反射』效果。
最終效果

實現(xiàn)
- 添加
CAReplicatorLayer
let replicatorLayer = CAReplicatorLayer.init()
// 復制圖層數(shù)量,包括本身
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,你就可以創(chuàng)建指定視圖(或整個視圖層次)內容的鏡像圖片,并且給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還有很多其他炫彩的效果,本人腦洞是不夠啊,逃~~ - 如有問題或建議請聯(lián)系我,博客地址甩出來http://chatwyn.ml/