GestureRecognizer 接受所有触摸输入
GestureRecognizer taking all Touch Input
我的情节提要中有这个设置。
在我的第一个 ViewController 场景中,我有一个来自 MapBox 的 MapView。在那里我放了一个 TextField (AddressTextField)。在那个 TextField 上,当触摸视图时,我是 运行 self.addressTextField.resignFirstResponder()
,但在那之后,无论是 mapview 还是其中或嵌入式 Segues 中的任何其他元素都不会对触摸或单击做出反应。大概是因为我没有完全理解First Responder
的系统吧。我很感谢你的帮助。
编辑 1:
我想我知道现在发生了什么,但我不知道如何解决它。当我将手势识别器添加到 View
(或添加到 mapView
时,其他 UIView 和 MapView 不再识别我的 Tap-Gestures。当我不添加识别器时,一切正常。似乎手势识别器正在识别我在 UIViews
或 MapView
上所做的每一次点击,因此无法识别其他手势。
编辑 2:
我刚刚在 dismissKeyboard()
中添加了一个 print()
。一旦在 MapView
或另一个 UIViews
上识别出任何触摸事件,就会调用 dismissKeyboard()
。所以我认为我对 Edit 1 的想法是正确的。有谁知道我该如何解决这个问题,这样不仅 dismissKeyboard()
被调用了?
一些代码:
func dismissKeyboard(){
self.addressTextField.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
dismissKeyboard()
return true
}
//Class (only partially)
class ViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate {
override func viewDidLoad(){
mapView.delegate = self
addressTextField.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
self.mapView.addGestureRecognizer(tap)
}
}
其他只是 @IBAction
链接到按钮或其他元素。
试试这个:
func dismissKeyboard(){
view.endEditing(true)
}
希望对您有所帮助!
在我知道了真正的问题之后,我就能够解决问题了。我声明了一个var keyboardEnabled
。然后我将这些行添加到我的 class.
class ViewController: UIViewController, UIGestureRecognizerDelegate {
var keyboardEnabled = false
override func viewDidLoad(){
super.viewDidLoad()
//Looks for single tap
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
self.mapView.addGestureRecognizer(tap)
}
/* Setting keyboardEnabled */
//Editing Target did end
@IBAction func editingTargetDidEnd(_ sender: Any) {
keyboardEnabled = false
}
//Editing TextField Started
@IBAction func editingAdressBegin(_ sender: Any) {
keyboardEnabled = true
}
//Call this function when the tap is recognized.
func dismissKeyboard() {
self.mapView.endEditing(true)
keyboardEnabled = false
}
//Implementing the delegate method, so that I can add a statement
//decide when the gesture should be recognized or not
//Delegate Method of UITapGestureRecognizer
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return keyboardEnabled
}
}
有了这个解决方案,keyboardEnabled
负责决定我的 UIGestureRecognizer
是否应该做出反应。如果 Recognizer 没有反应,Gesture 将简单地传递到 UIView 或我的 MapView 中的其他元素。
感谢您的所有回答!
我的情节提要中有这个设置。
在我的第一个 ViewController 场景中,我有一个来自 MapBox 的 MapView。在那里我放了一个 TextField (AddressTextField)。在那个 TextField 上,当触摸视图时,我是 运行 self.addressTextField.resignFirstResponder()
,但在那之后,无论是 mapview 还是其中或嵌入式 Segues 中的任何其他元素都不会对触摸或单击做出反应。大概是因为我没有完全理解First Responder
的系统吧。我很感谢你的帮助。
编辑 1:
我想我知道现在发生了什么,但我不知道如何解决它。当我将手势识别器添加到 View
(或添加到 mapView
时,其他 UIView 和 MapView 不再识别我的 Tap-Gestures。当我不添加识别器时,一切正常。似乎手势识别器正在识别我在 UIViews
或 MapView
上所做的每一次点击,因此无法识别其他手势。
编辑 2:
我刚刚在 dismissKeyboard()
中添加了一个 print()
。一旦在 MapView
或另一个 UIViews
上识别出任何触摸事件,就会调用 dismissKeyboard()
。所以我认为我对 Edit 1 的想法是正确的。有谁知道我该如何解决这个问题,这样不仅 dismissKeyboard()
被调用了?
一些代码:
func dismissKeyboard(){
self.addressTextField.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
dismissKeyboard()
return true
}
//Class (only partially)
class ViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate {
override func viewDidLoad(){
mapView.delegate = self
addressTextField.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
self.mapView.addGestureRecognizer(tap)
}
}
其他只是 @IBAction
链接到按钮或其他元素。
试试这个:
func dismissKeyboard(){
view.endEditing(true)
}
希望对您有所帮助!
在我知道了真正的问题之后,我就能够解决问题了。我声明了一个var keyboardEnabled
。然后我将这些行添加到我的 class.
class ViewController: UIViewController, UIGestureRecognizerDelegate {
var keyboardEnabled = false
override func viewDidLoad(){
super.viewDidLoad()
//Looks for single tap
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
self.mapView.addGestureRecognizer(tap)
}
/* Setting keyboardEnabled */
//Editing Target did end
@IBAction func editingTargetDidEnd(_ sender: Any) {
keyboardEnabled = false
}
//Editing TextField Started
@IBAction func editingAdressBegin(_ sender: Any) {
keyboardEnabled = true
}
//Call this function when the tap is recognized.
func dismissKeyboard() {
self.mapView.endEditing(true)
keyboardEnabled = false
}
//Implementing the delegate method, so that I can add a statement
//decide when the gesture should be recognized or not
//Delegate Method of UITapGestureRecognizer
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return keyboardEnabled
}
}
有了这个解决方案,keyboardEnabled
负责决定我的 UIGestureRecognizer
是否应该做出反应。如果 Recognizer 没有反应,Gesture 将简单地传递到 UIView 或我的 MapView 中的其他元素。
感谢您的所有回答!