我爲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()
}
這是完美的謝謝:) – RichAppz