ios - 图像在循环动画中移动 - Swift

标签 ios iphone swift xcode

在过去的 4 个月里,我一直致力于 swift 的开发,并在 2 周以来一直致力于这个闪屏。屏幕中间有一辆汽车,2 个图像(城市和地形)作为背景从右到左 (RtoL) 循环移动。检查下面从我的 android 应用程序中获取的 gif:

Taken from my Android app

我只能移动背景 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

  1. 为建筑背景
  2. 以树木为背景
  3. 为了汽车。

就像我创造了这个动画。 enter image description here

并且您想要从 LToRRToL 移动的图像必须在 ImageView 上加倍,以便您可以附加相同的图像(作为另一个实例)当图像的右边缘到达屏幕的右边缘时,到第一个结束

如下图所示。 enter image description here

并添加此代码以实现动画目的。

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/

相关文章:

ios - iTunes Connect 附上一份证明权利的契约(Contract)

swift - 在 iOS 13 上为选项卡栏设置自定义字体

ios - 在 Swift 中更改 iOS 应用程序的 TableView 和 TableView 单元格的位置和布局

objective-c - -> 在 Objective-C 中意味着什么

ios - 可达性问题 iOS9

iphone - 为 iPhone 转换为 sRGB?

iphone - detachNewThreadSelector 使应用程序随机崩溃

swift - 尝试在 Firestore 上进行另一个查询时出错

ios - Parse.com segue 不会传递数据

iphone - 分层 UIImageView 和 UIBezierPath