I animate a UIView on and off the screen by pulling up and swiping down. I use this code to do that:
private var _animator: AnyObject?
@available(iOS 10.0, *)
var animator: UIViewPropertyAnimator? {
get {
return _animator as? UIViewPropertyAnimator
}
set {
_animator = newValue
}
}
var haveTheySwiped = false
var haveTheyNotSwiped = true
@available(iOS 10.0, *)
func handlePan(recognizer: UIPanGestureRecognizer) {
let vel = recognizer.velocity(in: self)
switch recognizer.state {
case .began:
if vel.y < 0 && !haveTheySwiped {
animator = UIViewPropertyAnimator(duration: 1, curve: .easeOut, animations: {
self.backgroundImage.frame = self.backgroundImage.frame.offsetBy(dx: 0, dy: -603)
})
haveTheySwiped = true
isScanVisible = true
} else if vel.y > 0 && haveTheySwiped {
animator = UIViewPropertyAnimator(duration: 1, curve: .easeOut, animations: {
self.backgroundImage.frame = self.backgroundImage.frame.offsetBy(dx: 0, dy: 603)
})
haveTheySwiped = false
isScanVisible = false
}
animator?.pauseAnimation()
print(backgroundImage.frame.origin)
case .changed:
let translation = recognizer.translation(in: backgroundImage)
if vel.y < 0 {
animator?.fractionComplete = translation.y / -603
} else if vel.y > 0 && haveTheySwiped == true {
animator?.fractionComplete = translation.y / 603
}
case .ended:
animator?.continueAnimation(withTimingParameters: nil, durationFactor: 0)
case .possible: break
default: break
}
}
But, recently I have come across a bug. When I try to pull up, the UIView does not appear and then when I lift my finger up, the app crashes with this error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'It is an error to release a paused or stopped property animator. Property animators must either finish animating or be explicitly stopped and finished before they can be released.'
Has anyone come across this? Would love some help figuring out what is causing this to happen. Any help would be immensely appreciated. Thanks so much in advance!
Cheers, Thoe