2017-01-31 7 views
1

私はUIImageViewを使用して、画面の中央に表示されるようにアニメーション化している後端と先行端を持つ画面の幅を広げます。時間は、タイマーに関連しています。UIViewPropertyAnimatorを使用して制約アニメーションを一時停止し、再開する

私には、一時停止ボタンとしても機能する開始ボタンがあります。 アニメーションを最初から開始して問題なく一時停止することができますが、中断した場所から(またはまったく)再開することで問題が発生します。

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

以下のコードを追加しようとしましたが、違いはありませんでした。

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) 

誰かが私がそれを機能させるために必要なことについて少し明るくすることができます。

ありがとうございます。

答えて

1

私は問題を再開しようとすると、新しいアニメーションを作成すると思います。 startAnimation()を実行し、新しいアニメーションクラスを作成する必要があります。私はあなたがこのような考えを使用する必要があると思います:

@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

おかげで、そのトリックでした! –

関連する問題