通过下面的代码,我可以从左到右动画一个CALayer
let imageLayer = CALayer()
imageLayer.frame = CGRect(x: 0, y: 500, width: 100, height: 70)
imageLayer.contents = UIImage(named: "first_image")?.cgImage
let animation = CABasicAnimation()
animation.keyPath = "position.x"
animation.fromValue = 100
animation.toValue = 300
animation.duration = 3
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = true
animation.repeatCount = 2
imageLayer.add(animation, forKey: "basic")
self.view.layer.addSublayer(imageLayer)我想做的是
循环:1- "first_image“将从左向右移动。
和在
循环:2- "second_image“(这是另一幅图像)将从左向右移动,就像以前的循环一样。
在这里,循环: 1 +循环: 2实际上是一个动画,我希望这个动画发生很多次。如何做到这一点?如能提供任何协助,将不胜感激。
发布于 2017-11-15 09:14:20
当您需要从左到右&反之亦然的动画,您也可以使用UIView.anima..动画。
let testViewTowardsLeft = UIView(frame: CGRect(x: 0, y: 150, width: 50, height: 50))
testViewTowardsLeft.backgroundColor = .red
view.addSubview(testViewTowardsLeft)
UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
testViewTowardsLeft.frame = CGRect(x: 100, y: 150, width: 50, height: 50)
}, completion: nil)
let testViewTowardsRight = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
testViewTowardsRight.backgroundColor = .red
view.addSubview(testViewTowardsRight)
UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
testViewTowardsRight.frame = CGRect(x: 0, y: 100, width: 50, height: 50)
}, completion: nil)

https://stackoverflow.com/questions/47302753
复制相似问题