在过去的 4 个月里,我一直致力于 swift 的开发,并在 2 周以来一直致力于这个闪屏。屏幕中间有一辆汽车,2 个图像(城市和地形)作为背景从右到左 (RtoL) 循环移动。检查下面从我的 android 应用程序中获取的 gif:
我只能移动背景 RtoL,但只能移动一次。我尝试了其他动画选项,但似乎都不起作用。您可以看到这些图像比实际屏幕宽得多,所以应该没有任何问题。在下面检查我的方法:
func animate(_ image: UIImageView) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveLinear],
animations: {image.center.x -= 10},
completion: {_ in self.animate(image)})}
我唯一的要求是像上面的 gif 一样在循环中移动图像 RtoL,没有间隙。
谢谢:)
最佳答案
所以你必须移动背景,但又想让汽车看起来像在移动,所以对于这种情况: 你必须制作 3 个不同的 imageView
- 为建筑背景
- 以树木为背景
- 为了汽车。
并且您想要从 LToR 或 RToL 移动的图像必须在 ImageView 上加倍,以便您可以附加相同的图像(作为另一个实例)当图像的右边缘到达屏幕的右边缘时,到第一个结束
并添加此代码以实现动画目的。
func animateBackground() {
// Animate background
// cityImage is the visible image
// cityImage2 is the hidden image
UIView.animate(withDuration: 12.0, delay: 0.0, options: [.repeat, .curveLinear], animations: {
self.cityImage.frame = self.cityImage.frame.offsetBy(dx: -1 * self.cityImage.frame.size.width, dy: 0.0)
self.cityImage2.frame = self.cityImage2.frame.offsetBy(dx: -1 * self.cityImage2.frame.size.width, dy: 0.0)
}, completion: nil)
}
关于ios - 图像在循环动画中移动 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41358725/