2016-03-23 5 views
5

私はCollectionViewを持っており、ユーザーが選択したCollectionViewCell内にアニメーションを作成したいと思います。 animateKeyframesWithDurationを使用することを選択しました。これは、ステップごとにカスタムアニメーションを作成するためです。私のコードは次のようになります。iOSは完了前にanimateWithDurationを停止します

func animate() { 
    UIView.animateKeyframesWithDuration(1.0, delay: 0.0, options: .AllowUserInteraction, animations: {() -> Void in 
     UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.5, animations: {() -> Void in 
      // First step 
     }) 
     UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.5, animations: {() -> Void in 
      // Second step 
     }) 
     }) { (finished: Bool) -> Void in 
      if self.shouldStopAnimating { 
       self.loadingView.layer.removeAllAnimations() 
      } else { 
       self.animate() 
      } 
     } 
} 

これは、カスタムのCollectionViewCellが選択されたときに実行されます。 問題は、ある時点でアニメーションを強制的に停止させたいということです。しかし、私がそれをするとき、アニメーションが完全に停止していない、それはちょうど別のセル(おそらく最後に再使用されたセル)の残りのアニメーションを移動するだけですか?

私はなぜこれが起こっているのか理解できません。私はさまざまなアプローチを試しましたが、正常に完了ブロックに入る前にアニメーションを正常に終了する人はいません。

誰もがこれについて考えていますか?

+0

あなたは、このためのソリューションを見つけましたか? – swalkner

答えて

0

レイヤーからアニメーションを削除する代わりに、アニメーションを停止したいビューのプロパティを設定する非常に短いアニメーションを追加することができます。このような

何か:

if self.shouldStopAnimating { 
    UIView.animateWithDuration(0.01, delay: 0.0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: {() -> Void in 
     //set any relevant properties on self.loadingView or anything else you're animating 
     //you can either set them to the final animation values 
     //or set them as they currently are to cancel the animation 
    }) { (completed) -> Void in 
    }; 
} 

This answer may also be helpful.

+0

それでも動作しません。選択したセルで、アニメーションが正常に停止します。実際の問題は、それが別のセルで継続することです。私もUIView.setAnimationsEnabled(false)を試してみましたが、無駄です。これは本当に迷惑です – razvan

関連する問題