UIKeyboardWillShowNotification被调用三次

问题描述 投票:1回答:4

一旦键盘变得可见,我需要移动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 键盘消失了

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

ios swift keyboard nsnotifications
4个回答
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()
    })
}

0
投票

我建议用userInfo下通知的UIKeyboardAnimationDurationUserInfoKey字典中的键盘动画持续时间替换静态动画持续时间(0.4)。这样,您的动画将与键盘动画同步。您还可以使用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()
    })
}

0
投票

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


0
投票

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

Capitalizaion: None
Correction: No
Smart Dashes: No
Smart insert: No
Smart Quote: No
Spell Checking: No
© www.soinside.com 2019 - 2024. All rights reserved.