I have a UIImageView that stretches the width of the screen whose trailing and leading ends I'm animating to meet in the middle of the screen. The time it takes is in relation to a timer.
I have a start button that also functions as a pause button. I can get the animation to start initially and pause without problems, but getting it to resume again from where it left off (or even at all) is where I'm having trouble.
@IBAction func startBtnPressed(_ sender: AnyObject) {
if isStartBtnPressed == true {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(decreaseTime), userInfo: nil, repeats: true)
isStartBtnPressed = false
self.prepareTimeTrailing.constant = screenSize / 2
self.prepareTimeLeading.constant = screenSize / 2
animator = UIViewPropertyAnimator(duration: TimeInterval(timeSec), curve: .linear) {
self.view.layoutIfNeeded()
}
animator.startAnimation()
} else {
timer.invalidate()
isStartBtnPressed = true
animator.pauseAnimation()
}
}
I tried adding the piece of code below, but it didn't make a difference.
var animationTiming = UICubicTimingParameters.init(animationCurve: .linear)
animator.continueAnimation(withTimingParameters: animationTiming, durationFactor: 1.0)
UIView.animate(withDuration: TimeInterval(Float(timeSec)), delay: 0.0, options: [.curveLinear, .allowUserInteraction, .beginFromCurrentState], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
Hope someone can shed some light on what I need to do to make it work.
Thanks in advance.
I think the problem is the you create new animation when try to resume it. You need to perform startAnimation() with out create new animation class. I think you need to use some think like this:
@IBAction func startBtnPressed(_ sender: AnyObject) {
if animator == nil{
self.prepareTimeTrailing.constant = screenSize / 2
self.prepareTimeLeading.constant = screenSize / 2
animator = UIViewPropertyAnimator(duration: TimeInterval(timeSec), curve: .linear) {
self.view.layoutIfNeeded()
}
if isStartBtnPressed == true {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(decreaseTime), userInfo: nil, repeats: true)
isStartBtnPressed = false
animator.startAnimation()
} else {
timer.invalidate()
isStartBtnPressed = true
animator.pauseAnimation()
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.