gpt4 book ai didi

ios - 未调用手势识别器

转载 作者:行者123 更新时间:2023-11-28 05:27:57 24 4
gpt4 key购买 nike

我已经设置了一个手势识别器,用于在用户点击文本字段外部时关闭键盘。 DismissKeyboard 函数未被调用。

我是否设置了错误的观察者或者这是一个不同的问题?此外,这是一个正在被点击的表格 View 。

代码摘录

class CommentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillShow:"),
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillHide:"),
name: UIKeyboardWillHideNotification,
object: nil)
}

func keyboardFrameChanged(notification: NSNotification) {
println("keyboardFrameChanged")
let userInfo = notification.userInfo
let key = UIKeyboardFrameEndUserInfoKey
if let info = userInfo {
let frameValue = info[key] as! NSValue
let _frame = frameValue.CGRectValue()
}
}

func keyboardWillShow(notification: NSNotification) {
if keyboardDismissTapGesture == nil
{
println("dismiss")
keyboardDismissTapGesture = UITapGestureRecognizer(target: self, action: Selector("dismissKeyboard:"))
self.view.addGestureRecognizer(keyboardDismissTapGesture!)
}
}

func keyboardWillHide(notification: NSNotification) {
if keyboardDismissTapGesture != nil
{
println("test2")
self.view.removeGestureRecognizer(keyboardDismissTapGesture!)
keyboardDismissTapGesture = nil
}
}

func dismissKeyboard(sender: AnyObject) {
println("dismiss keyboard")
commentTextField.resignFirstResponder()
}

我在 dismissKeyboard 处设置了一个断点,但它甚至没有被调用。

输出

当我点击 TextView 并打开键盘时,这是输出

keyboardFrameChanged
keyboardFrameChanged
will show
dismiss

当我点击其他任何东西(试图关闭键盘)时,没有进一步的输出。

最佳答案

将手势识别器委托(delegate)设置为您自己并添加 UIGestureRecognizerDelegate 协议(protocol)。

func keyboardWillShow(notification: NSNotification) {
if keyboardDismissTapGesture == nil
{
println("dismiss")
keyboardDismissTapGesture = UITapGestureRecognizer(target: view, action: "dismissKeyboard:")
keyboardDismissTapGesture.delegate = self
self.view.addGestureRecognizer(keyboardDismissTapGesture!)
}
}

然后添加:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
return true
}

我的猜测是,您的表格 View 的手势干扰了新的 UITapGesture。试试这个解决方案,否则当检测到新的 UITapGesture 时,您的表格 View 的手势应该失败。

为了使 TableView 的 UITapGestureRecognizer 失败,我使用了以下代码:

if let recognizers = yourTableView.gestureRecognizers, let index = find(recognizers.map { $0 is UITapGestureRecognizer }, true) {
(recognizers[index] as! UIPanGestureRecognizer).requireGestureRecognizerToFail(keyboardDismissTapGesture)
}

这可能不是最优雅的做法,但当我想让 UIPanGestureRecognizer 失败时,它对我有用。我还没有使用 UITapGestureRecognizer 对其进行测试。

编辑:

if let recognizers = yourTableView.gestureRecognizers, let index = find(recognizers.map { $0 is UIGestureRecognizer }, true) {
(recognizers[index] as! UIGestureRecognizer).requireGestureRecognizerToFail(keyboardDismissTapGesture!)
}

关于ios - 未调用手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30247506/

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