2016-01-11 14 views
6

Stworzyłem animację rytmu serca dla Uibutton. Jednak nie ma możliwości zatrzymania tej animacji, ponieważ jest to nieskończona pętla kodu. Po majstrowaniu przy wielu blokach kodu animacji UIView nie udało mi się uzyskać UIViewAnimationOptions.Repeat, aby produkować to, czego potrzebuję. Gdybym mógł to zrobić, mógłbym po prostu button.layer.removeAllAnimations() usunąć animacje. Jaki jest sposób, aby napisać to, co pozwala na usunięcie animacji? Myślę, że może to być zegar, ale może to być trochę nieporządne w przypadku wielu animacji.UIButton Heartbeat Animation

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

} 

Odpowiedz

18

Działa to doskonale. Tłumienie i sprężynę trzeba nieco poprawić, ale to rozwiązuje problem. removeAllAnimations() usuwa animację i przywraca normalny stan przycisku.

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") 

Ten post był bardzo pomocny: CAKeyframeAnimation delay before repeating

+0

To jest doskonałe dzięki :) – RichAppz

0

na oryginalnym pytaniem Wspomniałeś, że chcesz, aby zatrzymać animację na komendę. Zakładam, że chciałbyś, żeby również zaczął na komendę. To rozwiązanie zrobi obie i jest dość proste.

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

Zadzwoń do cutAnim() w dowolnym miejscu, aby zatrzymać animację wewnątrz licznika czasu, jeśli chcesz.

aby uruchomić animację za pomocą zwykłego Mechanizm przyciskowy

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

nadzieję, że to pomaga.