gpt4 book ai didi

ios - SWIFT4 : How to have two tap gestureRecognizers work together

转载 作者:行者123 更新时间:2023-11-28 13:45:18 25 4
gpt4 key购买 nike

注意:我的手势识别器没有什么不同,并且在不同的 viewController 中。

TLDR: How can I do to have both my tap gesture recognisers to work?

我创建了一个 side menu从一些容器 View 中,我向它添加了一个手势识别器,允许我在点击时关闭它。然后我创建了另一个 ViewController,其中有一些文本字段。因为我希望键盘在点击时关闭,所以我什至添加了一个手势识别器,允许我在点击 View 时隐藏键盘。现在我注意到使用隐藏键盘的手势不会让我的侧边菜单在点击时隐藏:

侧边菜单 View Controller

  @IBOutlet weak var bigContainer: UIView!
@IBOutlet weak var sideMenuConstraint: NSLayoutConstraint!

var sideMenuOpen = false
var gesture : UITapGestureRecognizer?


override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(toggleSideMenu), name: NSNotification.Name("ToggleSideMenu"), object: nil)


gesture = UITapGestureRecognizer(target: self, action: #selector(ContainerViewController.toggleSideMenu))


}


@objc func toggleSideMenu() {
if sideMenuOpen {
sideMenuOpen = false
sideMenuConstraint.constant = -240
self.bigContainer.removeGestureRecognizer(gesture!)
} else {
sideMenuOpen = true
sideMenuConstraint.constant = 0
self.bigContainer.addGestureRecognizer(gesture!)

}
}

主视图 View Controller

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var menuOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(endEditingTapGesture)
}
@IBAction func toggleSideMenu(_ sender: Any) {
print("Toggle side menu")
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)

}
}

我尝试删除点击手势以隐藏我的 toggleSideMenu 按钮中的键盘,但它不起作用。

最佳答案

在您的MAIN VIEW CONTROLLER中替换为以下代码:

    var endEditingTapGesture:UIGestureRecognizer!

override func viewDidLoad() {
super.viewDidLoad()
endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false


NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notication:)), name: UIResponder.keyboardWillHideNotification, object: nil) //Add keyboard notification
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notication:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@IBAction func toggleSideMenu(_ sender: Any) {
print("Toggle side menu")
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)

}

//Keyboard method
@objc func keyboardWillHide(notication:Notification) {
view.removeGestureRecognizer(endEditingTapGesture)
}

@objc func keyboardWillShow(notication:Notification) {
view.addGestureRecognizer(endEditingTapGesture)
}

关于ios - SWIFT4 : How to have two tap gestureRecognizers work together,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55473196/

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