2017-07-22 18 views
1

UIViewをプルアップしてスワイプして画面をアニメーション表示します。私はそれを行うためにこのコードを使用します:一時停止したアニメーションとNSInternalInconsistencyExceptionのためにアプリケーションがクラッシュする

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 
     } 
} 

しかし、最近私はバグに出くわしました。私はプルアップしようとすると、UIViewのは表示されず、私は私の指を持ち上げるときに、このエラーでアプリがクラッシュ:

*** 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.'

は、誰もがこれに遭遇していますか?これが起こる原因を解明するのに役立つでしょう。どんな助けも非常に高く評価されます。事前にありがとう!

乾杯、 Thoe

+0

コメントを@solenoidおかげでそんなに!アニメーションをトリガするのではなく、実際にビューに結び付けられています。あなたが話していることを明確にすることはできますか? –

+0

エラーは、基本的には「ちょっと、私が別のスレッドで実行しているので、何らかの方法でコールバック(適切な方法で終了または取り消された)が行われるまで、私を解放しないでください」と言っています。 代わりに、ジェスチャー認識機能の入力を使用して、入力したビューを指で移動してください。私は今それをテストすることはできませんが、その周りの例があるはずです。 – solenoid

+0

@ソレノイドは私のコードのどの部分がそれをするのか知っていますか?ちょっと混乱します... haha​​ha –

答えて

0

の実装、その後stopAnimation(BOOL)(時:UIViewAnimatingPosition)finishAnimationは、問題をクリアする必要があります。

This class adopts the UIViewAnimating and UIViewImplicitlyAnimating protocols, which define the methods for starting, stopping, and modifying your animations. For more information about the methods of those protocols, see UIViewAnimating and UIViewImplicitlyAnimating .

https://developer.apple.com/documentation/uikit/uiviewanimating

func stopAnimation(Bool) Stops the animations at their current positions. Required.

func finishAnimation(at: UIViewAnimatingPosition) Finishes the animations and returns the animator to the inactive state. Required.

+0

お返事ありがとうございますが、私はそれを実装したいところはまだ不明です。 handlePan()関数は、今年の新しいタイプの関数です。それについてWWDCで話がありました。どこでstopAnimationとfinishAnimationを追加する必要がありますか? –

+0

私は便利なMacを持っていませんが、完成ハンドラをアニメーションhttps://developer.apple.com/documentation/uikit/uiviewpropertyanimator/1648373-addcompletionに追加してそこに配置します。また、アニメーションがキャンセルされる可能性のある場所に配置します。 – solenoid

関連する問題