我有一个图像,我使用此代码为viewDidAppear设置动画:
UIView.animateWithDuration(10.5,delay:0.0,options: [],animations:{ self.myImage.transform = CGAffineTransformMakeTranslation(0.0,200) },completion: nil)
我想点击myPauseButton时暂停动画,如果我再次点击它,则恢复动画.@H_301_5@
解决方法
暂停和恢复动画的2个功能,我从
here开始转换为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中初始化的动画:@H_301_5@
var pause = false override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) UIView.animateWithDuration(10.5,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) } }