如何暂停和恢复 UIView.animateWithDuration [英] How to pause and resume UIView.animateWithDuration
本文介绍了如何暂停和恢复 UIView.animateWithDuration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个图像,我在 viewDidAppear 中使用此代码对其进行动画处理:
I have an image, I animate it with this code, in viewDidAppear:
UIView.animateWithDuration(10.5, delay:0.0, options: [], animations:{
self.myImage.transform = CGAffineTransformMakeTranslation(0.0, 200)
}, completion: nil)
我想在点击 myPauseButton
时暂停动画,再次点击时恢复动画.
I want to pause the animation when I tap myPauseButton
, and resume the animation if I tap it again.
推荐答案
2 个暂停和恢复动画的函数,我取自 此处 并转换为 Swift.
2 functions to pause and resume animation, I take from here and convert to Swift.
func pauseLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
func resumeLayer(layer: CALayer) {
let pausedTime: CFTimeInterval = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
layer.beginTime = timeSincePause
}
我有一个按钮可以暂停或恢复在 viewDidLoad
中启动的动画:
I have a button to pause or resume the animation which is initiated in viewDidLoad
:
var pause = false
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 10.5) {
self.image.transform = CGAffineTransformMakeTranslation(0.0, 200)
}
}
@IBAction func changeState() {
let layer = image.layer
pause = !pause
if pause {
pauseLayer(layer)
} else {
resumeLayer(layer)
}
}
这篇关于如何暂停和恢复 UIView.animateWithDuration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文