我正在尝试让我的动画将屏幕从黑到白再到黑,并重复一定的次数。目前,在我的代码中,动画从黑色到白色,然后跳回到黑色。有没有办法反向运行动画,或者添加在第一个动画完成后运行的动画?
override func viewDidAppear(_ animated: Bool) {
let viewColorAnimator: UIViewPropertyAnimator = UIViewPropertyAnimator.runningPropertyAnimator(
withDuration: 4.0,
delay: 0.0,
options: [.curveEaseInOut],
animations: {
UIView.setAnimationRepeatCount(3);
self.lightView.backgroundColor = .white
})
viewColorAnimator.startAnimation()
}我尝试将这段代码添加到项目中,但结果是一样的:
viewColorAnimator.addCompletion {_ in
let secondAnimator = UIViewPropertyAnimator(duration: 4.0, curve: .linear) {
self.lightView.backgroundColor = .black
}
secondAnimator.startAnimation()
}编辑:我发现这是因为setAnimationRepeatCount,因为最后一次迭代工作正常。如何在没有重复次数的情况下多次运行动画?
发布于 2018-09-07 17:24:46
文档显示与方向相关的选项被忽略。你可以查看图片所附的here。
要实现反向动画,请执行以下操作:
创建一个动画制作属性。
private var animator: UIViewPropertyAnimator = {
let propertyAnimator = UIViewPropertyAnimator.runningPropertyAnimator(
withDuration: 4.0,
delay: 0.0,
options: [.curveEaseInOut, .autoreverse],
animations: {
UIView.setAnimationRepeatCount(3)
UIView.setAnimationRepeatAutoreverses(true)
self.lightView.backgroundColor = .white
})
return propertyAnimator
}()另外,我们需要在选项中提到.autoreverse。否则UIView.setAnimationRepeatAutoreverses(true)将无法工作。
发布于 2018-03-29 15:35:49
有一种简单的方法来运行动画。对于此方法:如果希望动画在完成后重复,可以添加.autoreverse和.repeat选项。如下所示:
UIView.animate(withDuration: TimeInterval(randomTime), delay: 0, options: [.repeat,.autoreverse], animations: {
//Animation code here
}, completion: {(bool)
//Do something after completion
})您可以将要执行的代码放在动画之后的完成块中。
并且,您可以使用计时器作为在特定时间内运行动画的一种方式。
发布于 2018-07-12 01:24:41
Xcode 9.4.1 (Swift 4.1.2)和Xcode 10 beta 3 (Swift 4.2)
下面是使用UIViewPropertyAnimator完成此操作的方法--基本上,我们只需将.repeat和.autoreverse添加到选项中。你走了99%的路:
override func viewDidAppear(_ animated: Bool) {
let viewColorAnimator: UIViewPropertyAnimator = UIViewPropertyAnimator.runningPropertyAnimator(
withDuration: 4.0,
delay: 0.0,
options: [.curveEaseInOut, .repeat, .autoreverse],
animations: {
UIView.setAnimationRepeatCount(3)
self.lightView.backgroundColor = .white
})
viewColorAnimator.startAnimation()
}https://stackoverflow.com/questions/49418128
复制相似问题