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

  

  

  

posted @ 2016-01-14 09:33  大雨不晴  阅读(8858)  评论(0编辑  收藏  举报