当模态视图被推送时,Swift animateWithDuration完成运行?

时间:2015-01-12 20:08:29

标签: ios objective-c iphone swift core-animation

以下代码为我的一个视图设置动画并具有完成块

UIView.animateWithDuration(0.5, delay: timeToShow, usingSpringWithDamping: 0.75, initialSpringVelocity: 2, options: nil, animations: {
                self.murmurComposeTextView.frame = oldFrame
                self.showNewMurmurView.frame = self.getLeavingBGFrame()
                }, completion: { finished in
                    self.pickerButtonBig.enabled = true
                    self.pickerButton.enabled = true
                    self.isShowingNewMurmur = false
                    self.murmurComposeTextView.becomeFirstResponder()
            })

如果我在运行时提出模态视图,那么请快速关闭它,我注意到两件事:

  1. 动画尚未完成,但照常进行(我想要)。
  2. 完成块已经运行,因为pickerButton已启用,self.murmurComposeTextView.becomeFirstResponder已运行等等。这是我不想要的。
  3. 有没有人知道这种行为是故意的,还是我有错误,并且无论如何都要修复它?谢谢!

1 个答案:

答案 0 :(得分:1)

传递到完成块的“已完成”Bool指示在调用完成块时动画是否实际完成。

所以你可以检查一下Bool来确定你想做什么。类似的东西:

UIView.animateWithDuration(0.5, delay: timeToShow, usingSpringWithDamping: 0.75, initialSpringVelocity: 2, options: nil, animations: {
            self.murmurComposeTextView.frame = oldFrame
            self.showNewMurmurView.frame = self.getLeavingBGFrame()
            }, completion: { finished in
               if (finished) {
                    self.pickerButtonBig.enabled = true
                    self.pickerButton.enabled = true
                    self.isShowingNewMurmur = false
                    self.murmurComposeTextView.becomeFirstResponder()
               } 
        })

文档: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/clm/UIView/animateWithDuration:animations:completion