gpt4 book ai didi

ios - CABasicAnimation:从左到右

转载 作者:行者123 更新时间:2023-11-30 12:00:27 27 4
gpt4 key购买 nike

通过下面的代码,我可以从左到右为 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)

我想做的是,在

Cycle: 1 - "first_image" will move from left to right

并在

Cycle: 2 - "second_image" (this is another image) will move from left to right like previous cycle.

这里Cycle: 1 + Cycle: 2实际上是一个动画,我希望这个动画发生多次。如何实现这一目标?任何帮助将不胜感激。

最佳答案

由于您需要从左到右的动画,反之亦然,您还可以使用 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)

enter image description here

关于ios - CABasicAnimation:从左到右,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47302753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com