UIKeyboardWillShowNotification被调用三次

时间:2016-03-16 10:35:33

标签: ios swift keyboard nsnotifications

一旦键盘变得可见,我需要移动UIView。但我现在面临的问题是,当我使用自定义键盘(例如SwiftKey)导致动画效果不佳时,我的UIKeyboardWillShowNotification被调用了三次。
有办法只处理最后一次通知吗?我可以很容易地躲避第一个,因为高度为0,但第二个看起来像一个有效的高度,我找不到如何解决这个问题的答案。
这是我到目前为止:

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillAppear(notification: NSNotification){
    print("keyboard appear")
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        print("with height: \(keyboardSize.height)")
        if keyboardSize.height == 0.0 {
            return
        }
        self.txtViewBottomSpace.constant = keyboardSize.height
        UIView.animateWithDuration(0.4, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })
    }
}

func keyboardWillDisappear(notification: NSNotification){
    print("Keyboard disappear")
    self.txtViewBottomSpace.constant = 0.0
    UIView.animateWithDuration(0.4, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })
}

我的日志输出是:

  

键盘出现
  高度:0.0
  键盘出现
  高度:216.0
  键盘出现
  身高:258.0
  键盘消失

那么有没有办法只处理第三个通知并“忽略”前两个?

4 个答案:

答案 0 :(得分:1)

将所有波纹管字段设置为NO可以解决此问题。

Capitalizaion: None
Correction: No
Smart Dashes: No
Smart insert: No
Smart Quote: No
Spell Checking: No

答案 1 :(得分:0)

我建议将静态动画持续时间(0.4)替换为键盘的动画持续时间,并在userInfo下的通知的UIKeyboardAnimationDurationUserInfoKey字典中返回。 这样,您的动画将与键盘动画同步。您还可以使用UIKeyboardAnimationCurveUserInfoKey键检索键盘使用的动画曲线。

func keyboardWillAppear(notification: NSNotification){
    print("keyboard appear")
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue;
        print("with height: \(keyboardSize.height)")
        if keyboardSize.height == 0.0 {
            return
        }
        self.txtViewBottomSpace.constant = keyboardSize.height
        UIView.animateWithDuration(animationDuration!, delay: 0.0, options: .BeginFromCurrentState, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })
    }
}

func keyboardWillDisappear(notification: NSNotification){
    print("Keyboard disappear")
    let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue;
    self.txtViewBottomSpace.constant = 0.0
        UIView.animateWithDuration(animationDuration!, delay: 0.0, options: .BeginFromCurrentState, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })
}

答案 2 :(得分:0)

更改通知名称UIKeyboardDidShowNotificationUIKeyboardDidHideNotification,然后解决问题

 override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillAppear:", name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillDisappear:", name: UIKeyboardDidHideNotification, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillAppear(notification: NSNotification){
    print("keyboard appear")
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        print("with height: \(keyboardSize.height)")
        if keyboardSize.height == 0.0 {
            return
        }
        self.txtViewBottomSpace.constant = keyboardSize.height
        UIView.animateWithDuration(0.4, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })
    }
}

func keyboardWillDisappear(notification: NSNotification){
    print("Keyboard disappear")
    self.txtViewBottomSpace.constant = 0.0
    UIView.animateWithDuration(0.4, animations: { () -> Void in
        self.view.layoutIfNeeded()
    })
}

答案 3 :(得分:0)

之所以这样,是因为键盘可以有不同的尺寸,尤其是第三方。因此,您收到的第一个通知将始终为默认系统高度,之后您收到的任何通知将包括第三方键盘扩展的新高度(如果已加载)。为了解决这个问题,在处理通知的方法中,您需要先获取高度,然后将其设置为默认高度(我认为226)。然后将变量设置为此第一个高度,然后对结果调用通知方法,您可以检查新高度是否大于原始高度,如果是,您可以找到增量,然后相应地重新调整帧。