我试图了解如何使用UIView.animateKeyframes
,但是我一生都无法理解如何计算时间/持续时间。
我正在努力实现以下目标: 我希望整个动画持续10秒钟,由...组成。
第一个addKeyframe运行时间为01秒,耗时1秒
second addKeyframe在02秒运行,耗时1秒
第三个addKeyframe运行时间为05秒,耗时2秒
第四个addKeyframe的运行时间为07秒,耗时1秒
第四个addKeyframe的运行时间为09秒,耗时1秒
fileprivate func animateWelcomeText() -> Void {
let duration: TimeInterval = 10
UIView.animateKeyframes(withDuration: duration, delay: 0, options: .calculationModeLinear, animations: {
UIView.addKeyframe(withRelativeStartTime: 1.0, relativeDuration: 1.0, animations: {
self.introTextLabel.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 2.0, relativeDuration: 1.0, animations: {
self.introTextLabelTwo.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 5.0, relativeDuration: 2.0, animations: {
self.introTextLabel.alpha = 0
self.introTextLabelTwo.alpha = 0
})
UIView.addKeyframe(withRelativeStartTime: 7.0, relativeDuration: 1.0, animations: {
self.introTextLabelThree.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 9.0, relativeDuration: 1.0, animations: {
self.introTextLabelFour.alpha = 1
})
}) { (_) in
print("Complete")
}
}
什么也没发生,然后动画完成,最后两个项目突然出现。
答案 0 :(得分:2)
这只是数学。只需将所有值除以持续时间即可获得相对值。
fileprivate func animateWelcomeText() -> Void {
let duration: TimeInterval = 10
UIView.animateKeyframes(withDuration: duration, delay: 0, options: .calculationModeLinear, animations: {
UIView.addKeyframe(withRelativeStartTime: 1/duration, relativeDuration: 1/duration, animations: {
self.introTextLabel.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 2/duration, relativeDuration: 1/duration, animations: {
self.introTextLabelTwo.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 5/duration, relativeDuration: 2/duration, animations: {
self.introTextLabel.alpha = 0
self.introTextLabelTwo.alpha = 0
})
UIView.addKeyframe(withRelativeStartTime: 7/duration, relativeDuration: 1/duration, animations: {
self.introTextLabelThree.alpha = 1
})
UIView.addKeyframe(withRelativeStartTime: 9/duration, relativeDuration: 1/duration, animations: {
self.introTextLabelFour.alpha = 1
})
}) { (_) in
print("Complete")
}
}
您的值不等于x(10)。该方法需要1。