2016-01-11 7 views
6

私はUIButtonのハートビートアニメーションを作成しました。しかし、無限のコードループであるため、このアニメーションを停止する方法はありません。多数のUIViewアニメーションコードブロックを修正した後、私は必要なものを生成するためにUIViewAnimationOptions.Repeatを得ることができませんでした。もし私がそれを行うことができたら、私は単にbutton.layer.removeAllAnimations()アニメーションを削除することができます。これを書いてアニメーションを削除する方法は何ですか?私はおそらくタイマーを考えていますが、それは多種のアニメーションが進行しているので面倒です。UIButtonハートビートアニメーション

func heartBeatAnimation(button: UIButton) { 

    button.userInteractionEnabled = true 
    button.enabled = true 

    func animation1() { 

     UIView.animateWithDuration(0.5, delay: 0.0, options: [], animations: {() -> Void in 

      button.transform = CGAffineTransformMakeScale(2.0, 2.0) 
      button.transform = CGAffineTransformIdentity 

     }, completion: nil) 

     UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: {() -> Void in 

      button.transform = CGAffineTransformMakeScale(2.0, 2.0) 
      button.transform = CGAffineTransformIdentity 

      }) { (Bool) -> Void in 

       delay(2.0, closure: {() ->() in 

        animation2() 

       })  
     } 
    } 

    func animation2() { 

     UIView.animateWithDuration(0.5, delay: 0.0, options: [], animations: {() -> Void in 

      button.transform = CGAffineTransformMakeScale(2.0, 2.0) 
      button.transform = CGAffineTransformIdentity 

      }, completion: nil) 

     UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: {() -> Void in 

      button.transform = CGAffineTransformMakeScale(2.0, 2.0) 
      button.transform = CGAffineTransformIdentity 

      }) { (Bool) -> Void in 

       delay(2.0, closure: {() ->() in 

        animation1() 

       }) 
     } 
    } 

    animation1() 

} 

答えて

18

これは完全に機能します。ダンピングとスプリングはちょっと微調整する必要がありますが、これで問題は解決します。 removeAllAnimations()はアニメーションを消去し、ボタンを通常の状態に戻します。

button.userInteractionEnabled = true 
button.enabled = true 

let pulse1 = CASpringAnimation(keyPath: "transform.scale") 
pulse1.duration = 0.6 
pulse1.fromValue = 1.0 
pulse1.toValue = 1.12 
pulse1.autoreverses = true 
pulse1.repeatCount = 1 
pulse1.initialVelocity = 0.5 
pulse1.damping = 0.8 

let animationGroup = CAAnimationGroup() 
animationGroup.duration = 2.7 
animationGroup.repeatCount = 1000 
animationGroup.animations = [pulse1] 

button.layer.addAnimation(animationGroup, forKey: "pulse") 

この投稿は、とても役に立ちました:CAKeyframeAnimation delay before repeating

+0

これは完璧な感謝です:) – RichAppz

0

あなたはアニメーションがコマンドで停止すると述べたあなたの元の質問には。私はあなたがそれもコマンドで始めることを望むと思います。このソリューションは両方を行い、非常に簡単です。

func cutAnim(){ 
    for view in animating { 
     ///I use a UIView because I wanted the container of my button to be animated. UIButton will work just fine too. 
     (view.value as? UIView)?.layer.removeAllAnimations() 
    } 
} 

func pulse(button: UIButton, name: String){ 
    ///Here I capture that container 
    let container = button.superview?.superview 
    ///Add to Dictionary 
    animating[name] = container 
    cutAnim() 
    UIView.animate(withDuration: 1, delay: 0.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse, .allowUserInteraction], animations: { 
     container?.transform = CGAffineTransform(scaleX: 1.15, y: 1.15) 
     ///if you stop the animation half way it completes anyways so I want the container to go back to its original size 
     container?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) 
    }, completion: nil) 
} 

必要に応じて、タイマー内でアニメーションを停止するには、どこでもcutAnim()を呼び出します。アニメーションは、通常のボタンアクション

@IBAction func buttonWasTappedAction(_ sender: Any) { 
    pulse(button: sender as! UIButton, name: "nameForDictionary") 
    } 

を使用開始するには

は、この情報がお役に立てば幸いです。