2017-01-31 3 views
1

Ich habe eine UIImageView, die die Breite des Bildschirms streckt, deren nachlaufende und führende Enden ich animiere, in der Mitte des Bildschirms zu treffen. Die Zeit ist in Bezug auf einen Timer.Anhalten und Fortsetzen von Constraint-Animationen mit UIViewPropertyAnimator

Ich habe eine Start-Taste, die auch als Pause-Taste fungiert. Ich kann die Animation starten und pausieren ohne Probleme, aber es wieder zu bekommen, wo es aufgehört hat (oder sogar überhaupt) ist, wo ich Probleme habe.

@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() 
    } 
} 

Ich habe versucht, den Code unten hinzuzufügen, aber es machte keinen Unterschied.

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) 

Hoffe jemand kann etwas Licht auf das, was ich tun muss, damit es funktioniert.

Vielen Dank im Voraus.

Antwort

1

Ich denke, das Problem ist, dass Sie neue Animation erstellen, wenn Sie versuchen, es fortzusetzen. Sie müssen startAnimation() ohne neue Animationsklasse erstellen. Ich denke, dass Sie etwas denken müssen wie folgt:

@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() 
    } 
} 
+0

Danke Mann, das hat den Trick! –

Verwandte Themen