Swift - 动画 UIImageView 图像

Jor*_*rdy 3 xcode uiimageview ios swift swift3

这只是我遇到的一个关于为我当前的应用程序设置设置移动背景图像动画的快速问题。本质上,我想让当前静止的背景图像在我的视图中从左到右无休止地滚动;但我正在努力将它们拼凑在一起。我从各种其他来源编写了这个当前不起作用的函数,而且我真的不知道如何或为什么。

func animate(_ image: UIImageView) {
        UIView.animate(withDuration: 5, delay: 0, options: .curveLinear, animations: {
            image.transform = CGAffineTransform(translationX: -100, y: 0)
        }) { (success: Bool) in
            image.transform = CGAffineTransform.identity
            self.animate(image)
        }
    }
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助/协助!

Shu*_*aik 5

您不需要使用CAffineTranforminside animateWithDuration,您可以简单地定义新中心,例如:

let oldCenter = image.center
let newCenter = CGPoint(x: oldCenter.x - 100, y: oldCenter.y)

UIView.animate(withDuration: 1, delay: 0, options: .curveLinear, animations: {
  image.center = newCenter
}) { (success: Bool) in
  print("Done moving image")
  }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。