我有一个UIButton
上载文件,我正在尝试实现一个动画,其中有一组单独的点在按钮下方水平排列,并且最左边的点淡入/淡出,然后使其旁边的点淡入/一直到最右边的点,然后从左边重新开始循环。我可以将其用于一个点,但是对多个点执行此操作的最有效方法是什么?
func dotAnimation(){
let xCoord = self.recButt.frame.origin.x + 25
let yCoord = self.recButt.frame.origin.y + 5
let radius = 3
let dotPath = UIBezierPath(ovalIn: CGRect(x: Int(xCoord), y: Int(yCoord), width: radius, height: radius))
let layer = CAShapeLayer()
layer.path = dotPath.cgPath
layer.strokeColor = UIColor(red: 95.00/255, green: 106.00/255, blue: 255/255, alpha: 1.00).cgColor
self.view.layer.addSublayer(layer)
let animation : CABasicAnimation = CABasicAnimation(keyPath: "opacity");
animation.autoreverses = true
animation.fromValue = 0
animation.toValue = 1
animation.duration = 2.0
layer.add(animation, forKey: nil)
}
最佳答案
您正在描述一个CAReplicatorLayer。这是一种使用条形代替点来完成您想要做的事情:
这是代码。请注意,只有一个红色条形图层;复制到五个,以及偏移淡入淡出动画由包含的复制器层处理:
let lay = CAReplicatorLayer()
lay.frame = CGRect(0,0,100,20)
let bar = CALayer()
bar.frame = CGRect(0,0,10,20)
bar.backgroundColor = UIColor.red.cgColor
lay.addSublayer(bar)
lay.instanceCount = 5
lay.instanceTransform = CATransform3DMakeTranslation(20, 0, 0)
let anim = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
anim.fromValue = 1.0
anim.toValue = 0.2
anim.duration = 1
anim.repeatCount = .infinity
bar.add(anim, forKey: nil)
lay.instanceDelay = anim.duration / Double(lay.instanceCount)
// and now just add `lay` to the interface
关于ios - 淡入/淡出一系列点的动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42399720/