iOS 停止UIView的block動畫


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

  

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM