如何暂停和恢复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(), fromLayer: 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(), fromLayer: nil) - pausedTime
layer.beginTime = timeSincePause
}
我有一个暂停或恢复动画的按钮,该动画在 viewDidLoad
:
I have a button to pause or resume the animation which is initialed in viewDidLoad
:
var pause = false
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
UIView.animateWithDuration(10.5, delay:0.0, options: [], animations:{
self.image.transform = CGAffineTransformMakeTranslation(0.0, 200)
}, completion: nil)
}
@IBAction func changeState() {
let layer = image.layer
pause = !pause
if pause {
pauseLayer(layer)
} else {
resumeLayer(layer)
}
}
这篇关于如何暂停和恢复UIView.animateWithDuration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文