下面的代码为我的一个 View 设置了动画并有一个完成 block
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()
})
如果我在运行时呈现模态视图,然后快速关闭它,我注意到两件事:
- 动画尚未完成,但照常进行(这是我想要的)。
- 完成 block 已经运行,因为启用了 pickerButton,self.murmurComposeTextView.becomeFirstResponder 已经运行,等等。这是我不想要的。
有谁知道这种行为是否是故意的,或者我是否有错误,以及是否有办法修复它?谢谢!
最佳答案
传递到完成 block 的“finished” bool 值指示调用完成 block 时动画是否实际完成。
所以你可以检查 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()
}
})
关于ios - Swift animateWithDuration Completion 在推送模态视图时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27909933/