Skip to content

Instantly share code, notes, and snippets.

@AnselmeKotchap
Last active July 21, 2016 12:55
Show Gist options
  • Save AnselmeKotchap/c773d6efb85bcfd4546703754ca20318 to your computer and use it in GitHub Desktop.
Save AnselmeKotchap/c773d6efb85bcfd4546703754ca20318 to your computer and use it in GitHub Desktop.
//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
}
//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)
}
}
//sourec: http://stackoverflow.com/questions/33994520/how-to-pause-and-resume-uiview-animatewithduration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment