【问题标题】:Swift - animating ImageViewSwift - 动画 ImageView
【发布时间】:2019-07-25 00:17:26
【问题描述】:

我遇到的一个快速问题是为我当前的应用程序设置动画移动背景图像视图。本质上,我想让我当前静止的背景图像视图无休止地向上滚动到开始位置;但我正在努力将它们拼凑在一起。有人可以帮我吗?

 class Flash: UIViewController {
@IBOutlet weak var arrowImage: UIImageView!

    var arrow1: UIImage!
    var arrow2: UIImage!
    var arrow3: UIImage!
    var images: [UIImage]!
    var animatedImage: UIImage!
    override func viewDidLoad() {
        super.viewDidLoad()
        toggleFlash()
        arrow1 = UIImage(named: "Arrow1.png")
        arrow2 = UIImage(named: "Arrow2.png")
        arrow3 = UIImage(named: "Arrow3.png")
        images = [arrow1, arrow2, arrow3]
       animatedImage = UIImage.animatedImage(with: images, duration: 1.0)
        arrowImage.image = animatedImage

    }

    func blinkScreen(){

    UIView.animate(withDuration: 1, delay: 0, options: [.repeat, .curveLinear], animations: {

    }) { _ in
    }
}

【问题讨论】:

  • 您能否详细说明“基本上我想让我当前静止的背景图像视图无休止地向上滚动到起始位置”的意思。您提供的代码将在很长一段时间内将图像视图向上移动 10 点,这意味着图像视图将有效地静止。
  • 假设我有一个带有四个箭头的图像视图,它们都指向上方,我希望我的图像视图保持静止,但在我的视图顶部设置动画并返回到开始位置并重复动画。

标签: swift image jquery-animate transform


【解决方案1】:

好的,我想我明白你的目的是什么。

首先,duration 参数是动画每次迭代的持续时间。其次, UIView.animate(...) 不会重复动画,除非您将 .repeat 选项添加到 options 参数。

所以要实现更接近您想要的东西,请将您的代码更新为:

func blinkScreen()
    UIView.animate(withDuration: 1, delay: 0, options: [.repeat, .curveLinear], animations: {
        self.imageV.transform = CGAffineTransform(translationX: 0, y: -10)
    }) { _ in
        self.imageV.transform = .identity
    }
}

如果您想为上下移动设置动画,请使用 .autoreverse 选项(也可以使用 .curveEaseInOut):

func blinkScreen()
    UIView.animate(withDuration: 1, delay: 0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
        self.imageV.transform = CGAffineTransform(translationX: 0, y: -10)
    }) { _ in
        self.imageV.transform = .identity
    }
}

【讨论】:

  • 谢谢,该动画有效,但现在我有一个带有单个图像视图的视图控制器。我希望我的图像视图包含一组具有相同背景的图像。但是当动画开始时,我希望我的图像被新的图像替换,而旧的图像会随着曲线线性消失。我已经用新代码更新了我的旧代码。
猜你喜欢
  • 1970-01-01
  • 2011-01-03
  • 2014-10-30
  • 2011-01-17
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多