我为 Uber 创建了一个自定义控件,例如 OTP TextField。我想消耗控件中的触摸,而不是让它通过 UIResponder 链传播。所以我已经重写了apple documentation.中描述的方法。
extension BVAPasswordTextField {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
becomeFirstResponder()
}
override func touchesMoved(Set<UITouch>, with: UIEvent?) {
}
override func touchesEnded(Set<UITouch>, with: UIEvent?) {
}
override func touchesCancelled(Set<UITouch>, with: UIEvent?) {
}
override func touchesEstimatedPropertiesUpdated(Set<UITouch>) {
}
}
现在,在某些 View Controller 中,当用户点击自定义控件之外的任何位置时,我想关闭键盘。
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecogniser = UITapGestureRecognizer(target: self, action: #selector(ViewController.backgroundTapped))
tapGestureRecogniser.numberOfTapsRequired = 1
view.addGestureRecognizer(tapGestureRecogniser)
// Do any additional setup after loading the view, typically from a nib.
}
@objc func backgroundTapped() {
self.view.endEditing(true)
}
但是每当我点击文本字段时,backgroundTapped也会被调用。
注意:- 它是一个控件,您可以根据枚举值创建不同的 UI 组件来接受输入。所以这个控件可以在整个团队之间共享...我不会是唯一使用它的人...所以我希望它的行为与触摸场景中的 UITextfield 完全相同
最佳答案
您可以使用 UITapGestureRecognizerDelegate
处理点击手势。它允许您决定是否应该开始手势。在您的情况下,应该根据触摸的位置来完成。
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let location = gestureRecognizer.location(in: self.view)
// return true is location of touch is outside our textField
return !textField.frame.contains(location)
}
}
只需确保您已在 viewDidLoad
处为手势设置了一个委托(delegate)
tapGestureRecogniser.delegate = self
更新
如果我的设置正确,您将拥有一个 View 和一些用于输入的 subview 。当这个super-view
被触摸时,你想要resignFirstResponder
。在这种情况下,您可以像这样使用 gestureRecognizerShouldBegin
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let location = gestureRecognizer.location(in: self.view)
for subview in self.view.subviews {
if subview.frame.contains(location) {
return false
}
}
return true
}
在我看来,如果您想以特定方式处理某些 View
交互,则需要使用该特定 View
来完成。您现在所做的感觉就像尝试使用另一个 View 来更改一个 View
上的行为。
关于ios - 防止触摸从 UIControl 传递到 ParentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52540511/