图像移动循环动画 - 斯威夫特

Vip*_*egi 4 iphone xcode ios swift

我在过去的4个月里一直在使用swift工作,并在2周后开始使用这个闪屏.屏幕中间有车,2个图像(城市和地形)从右到左(RtoL)循环移动作为背景.检查以下从我的Android应用程序中获取的gif:

取自我的Android应用程序

我只能移动背景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)})}
Run Code Online (Sandbox Code Playgroud)

我唯一的要求就是像上面的gif一样在循环中移动图像RtoL.

谢谢 :)

Rah*_*lid 8

所以它就像你必须移动背景但想让它看起来像汽车正在移动,所以对于这种情况:你必须制作3个不同的imageViews

  1. 对于建筑背景
  2. 对于树木背景
  3. 对于汽车.

就像我创造了这个动画一样. 在此输入图像描述

要从LToR或RToL移动的图像,您必须在图像视图上加倍,这样当图像的右边缘到达右边缘时,您可以将相同的图像(作为另一个实例)附加到第一个图像的末尾的屏幕

像这样如下图所示. 在此输入图像描述

并为动画添加此代码.

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)

}
Run Code Online (Sandbox Code Playgroud)