gpt4 book ai didi

ios - keyboardwillshow 在水平旋转和垂直方向上被调用两次甚至三次(vert)

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:17 28 4
gpt4 key购买 nike

我试图获取键盘测量值,但在旋转过程中,我总是在打印部分遇到 2 个键盘测量到水平位置的通知和三个到垂直位置的通知。

  override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.tabBar.isHidden = true

collectionView?.backgroundColor = UIColor.white
collectionView?.register(ChatLogMessageCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.alwaysBounceVertical = true

collectionView?.showsVerticalScrollIndicator = true


collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0,left: 0,bottom: (tabBarController?.tabBar.frame.size.height)!,right: 0)
view.addSubview(messageInputContainerView)
view.addConstraintsWithFormat("H:|[v0]|", views: messageInputContainerView)
view.addConstraintsWithFormat("V:[v0(48)]|", views: messageInputContainerView)
setupInputComponents()
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

}
deinit {
print("Remove NotificationCenter Deinit")
NotificationCenter.default.removeObserver(self)
}
func handleKeyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
print(keyboardFrame)
}
}

到底发生了什么:当我第一次点击 textField 时:只有一个字符串 -Optional((0.0, 451.0, 375.0, 216.0)) - 正如预期的那样。

然后当旋转过程发生时:有两个字符串 Optional((0.0, 213.0, 667.0, 162.0))可选((0.0, 213.0, 667.0, 162.0))

毕竟,当我将它恢复到垂直位置时:甚至会出现另外三个字符串:Optional((0.0, 667.0, 375.0, 0.0))可选((0.0,667.0,375.0,0.0))可选((0.0, 451.0, 375.0, 216.0))

在其他讨论中有人说“这是正常行为,因为键盘在旋转前下降,旋转后又上升”但我没有在文档和其他任何地方找到任何确认。为什么真的会发生?拜托,我真的很困惑(

最佳答案

您可以通过将 Int 设置为零来解决此问题,然后当用户弹出键盘时,将 Int 设置为 1(或其他一些数字),如下所示:

var keyBoardCount:Int = 0 
func handleKeyboardNotification(notification: NSNotification) {
if keyBoardCount == 0 { //If the keyboard hasnt been raised, raise it.
if let userInfo = notification.userInfo {
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
print(keyboardFrame)
keyBoardCount = 1 //once you raise the keyboard, set the counter to 1
}
}
}

然后,当用户接受输入的字符串(IE 完成编辑文本字段)时,将 keyBoardCount 设置回零以重置整个过程。

通过这样做,您可以防止函数的多次触发。

关于ios - keyboardwillshow 在水平旋转和垂直方向上被调用两次甚至三次(vert),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48561342/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com