持续动画导致移动到另一个视图控制器时出现延迟

时间:2016-08-17 13:43:01

标签: ios animation swift2

好吧所以我有button出现并隐藏键盘(当键盘出现时它也会出现,当我解除键盘时它也会随键盘消失)

所以,当我按下button打开另一个View控制器时,转换不会立即开始,有一点延迟(这不好)

我的按钮的动画:

    func keyboardWillShow(notification:NSNotification) {
    let userInfo:NSDictionary = notification.userInfo!
    let duration: Double = (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double)
    let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.CGRectValue()
    self.nextButtonConstraint.constant  = keyboardRectangle.height
    UIView.animateWithDuration(duration) {
        self.nextButton.layoutIfNeeded()
    }

}

func keyboardWillHide(notification:NSNotification) {
    let duration: Double = (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double)
     self.nextButtonConstraint.constant  = -50
    UIView.animateWithDuration(duration) {
          self.nextButton.layoutIfNeeded()

    }
}

我厌倦了在按下按钮时禁用动画,但它没有奏效:

    @IBAction func nextButtonAction(sender: AnyObject) {

    UIView.setAnimationsEnabled(false)
    performSegueWithIdentifier("nextToPasswordSegue", sender: self)
     UIView.setAnimationsEnabled(true)
}

根据similar question的答案:

显然有一个动画正在同时发生,导致问题。

任何线索如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,当我试图移动到下一个控制器时出现了延迟。对我来说,修复它的方法是在执行segue之前隐藏键盘(resignFirstResponder)。然后,您将要在主队列上运行perform segue行,以确保它立即触发。这是一个例子:

textField.resignFirstResponder()
dispatch_async(dispatch_get_main_queue()) {
    performSegueWithIdentifier("nextToPasswordSegue", sender: self)
}