【问题标题】:animateWithDuration Completion block fires over and over againanimateWithDuration 完成块一遍又一遍地触发
【发布时间】:2016-03-23 00:39:07
【问题描述】:

我正在制作一个小动画,当该动画结束时,我希望应用程序自动推送下一个ViewController。好吧,ViewController 被连续推了 8 次!真是太疯狂了。

代码如下:

     if CGRectIntersectsRect(whichButton.frame, targetZoneImgView.frame) {
        panGesture.cancelsTouchesInView = true
        let buttonSnapX = UIScreen.mainScreen().bounds.width / 2.0 - 35.0
        let buttonSnapY = UIScreen.mainScreen().bounds.height - 122.0

        UIView.animateWithDuration(0.75, delay: 0.0, 
                usingSpringWithDamping: 0.1, 
                 initialSpringVelocity: 0.0, 
                               options: UIViewAnimationOptions.CurveEaseOut, 
                            animations: {
                                 myButton.frame.origin.x = buttonSnapX
                                 myButton.frame.origin.y = buttonSnapY
                            }, completion: { (finished: Bool) -> Void in
                                  print("Animation done!")
                                  let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                                  let coursesScreen = storyBoard.instantiateViewControllerWithIdentifier("coursesScreen") as! CoursesVC                  
                                  self.navigationController!.pushViewController(coursesScreen, animated: true)
                               }
                           )

print("Animation done!") 语句也会连续触发 8-9 次,因此它与我推送 ViewController 的事实无关。我取出了ViewController 代码 - 它仍在发生。

基本上,我在completion 中输入的任何东西都会连续发射很多次。

世界上发生了什么事?

【问题讨论】:

  • 我以前从未亲自遇到过这种情况,但是如果您不使用弹簧动画,请看看它是否会停止。 (只是一个经典的animateWithDuration:animations:completion:)如果能解决它,那么我敢打赌苹果在他们的春季动画中做了一些非常愚蠢的事情!

标签: ios animation completionhandler animatewithduration


【解决方案1】:

您可以使用false 变量的finished 值进入完成块。只需添加一个基本检查:

UIView.animateWithDuration(0.75, delay: 0.0, 
                usingSpringWithDamping: 0.1, 
                 initialSpringVelocity: 0.0, 
                               options: UIViewAnimationOptions.CurveEaseOut, 
                            animations: {
                                 myButton.frame.origin.x = buttonSnapX
                                 myButton.frame.origin.y = buttonSnapY
                            }, completion: { (finished: Bool) -> Void in
                                 if finished {
                                    print("Animation done!")
                                    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                                    let coursesScreen = storyBoard.instantiateViewControllerWithIdentifier("coursesScreen") as! CoursesVC                  
                                    self.navigationController!.pushViewController(coursesScreen, animated: true)
                                 } else {
                                    print("Animating...")
                                 }
                            }
                          )

【讨论】:

  • 我在发布问题之前尝试了if finished 位 - 它没有用。但是既然你提出了建议,我就进行了更多的探索......我认为问题更多地与拖动手势触发整个事情这一事实有关。当我将一个对象拖到另一个对象中时——这就是动画开始的时候。所以它是if CGRectIntersectsRect。也许原始的拖动手势没有被取消,所以它会导致事情一次又一次地被触发。我尝试取消触摸事件 - 不走运。但是,如果您现在知道这一切都与手势有关,也许您还有其他想法?
  • 查看原始问题中的编辑代码。我在整件事之前添加了if CGRectIntersectsRect 位。
猜你喜欢
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
相关资源
最近更新 更多