iOS停止UIView的block動畫的方法
動畫執行如下:
UIView.animateWithDuration(animationDuringTime, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { [weak self]() -> Void in
// do animation
animateView?.contentOffset.y = 0
}, completion: { [weak self](finished: Bool) -> Void in
NSLog("completion")
})
如果在動畫過程中突然想停止動畫,需要執行如下方法
// 這里的 animateView 要為 UIView block animation中進行動畫效果的view animateView.layer.removeAllAnimations()
方法執行之后,會UIView block動畫會立刻進入completion流程,傳入的 finished 值為 false, 可以在代碼中通過判斷 finished 為 true 或者 false 來區分動畫是正常完成還是被停止的。
UIView.animateWithDuration(animationDuringTime, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { [weak self]() -> Void in
// do animation
animateView?.contentOffset.y = 0
}, completion: { [weak self](finished: Bool) -> Void in
if !finished{
NSLog("animation stop")
}else{
NSLog("completion")
}
})
