我的应用程序中有一个非常奇怪的问题。我用UIViewPropertyAnimator
动画来改变里面的图像UIImageView
。
听起来像是微不足道的任务,但出于某种原因,我的视图最终会立即改变图像,所以我最终得到的图像以光速闪烁,而不是给定的持续时间和延迟参数。
这是代码:
private lazy var images: [UIImage?] = [
UIImage(named: "widgethint"),
UIImage(named: "shortcuthint"),
UIImage(named: "spotlighthint")
]
private var imageIndex = 0
private var animator: UIViewPropertyAnimator?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animator = repeatingAnimator()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
animator?.stopAnimation(true)
}
private func repeatingAnimator() -> UIViewPropertyAnimator {
return .runningPropertyAnimator(withDuration: 2, delay: 6, options: [], animations: {
self.imageView.image = self.images[self.imageIndex]
}, completion: { pos in
if self.imageIndex >= self.images.count - 1 {
self.imageIndex = 0
} else {
self.imageIndex += 1
}
self.animator = self.repeatingAnimator()
})
}
因此,根据代码动画应该需要 2 秒才能完成并在 6 秒延迟后开始,但它会立即开始并需要几毫秒才能完成,所以我最终得到了可怕的幻灯片。为什么会发生?
我也尝试使用UIViewPropertyAnimator(duration: 2, curve: .linear)
and 调用repeatingAnimator().startAnimation(afterDelay: 6)
,但结果是一样的。