如果我在 swift 中添加 UITapGestureRecognizer,UIButton 单击事件将无法正常工作
UIButton Click Event not working if I add UITapGestureRecognizer in swift
在我的登录页面上,如果我专注于 UITextEdit 以输入电子邮件和密码,则会出现键盘并向上滚动登录页面。如果我触摸键盘外部,我会添加此代码以删除键盘。
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
})
}
}
在我的 viewDidLoad() 函数中,我是这样添加的。
override func viewDidLoad()
{
super.viewDidLoad()
//TODO
self.hideKeyboardWhenTappedAround()
...
}
它运行良好,但是如果我在键盘仍处于打开状态时单击登录按钮,dismisskeyboard() 功能有效但 btnClick 功能无效。
@IBAction func LoginBtnClick(_ sender: Any)
{
print("loginBtn")
//...
}
我该如何解决这个问题?
不要将手势添加到主视图 viewController 在 UIButton 下面添加一个全屏子视图并向其添加手势
我已经使用以下代码对其进行了测试:
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
})
}
}
class ViewController: UIViewController {
fileprivate let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 400, height: 50))
fileprivate let button = UIButton(frame: CGRect(x: 20, y: 200, width: 150, height: 50))
override func loadView() {
super.loadView()
view.addSubview(textField)
textField.backgroundColor = .lightGray
view.addSubview(button)
button.setTitle("Press me", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.addTarget(self, action: #selector(a), for: .touchUpInside)
hideKeyboardWhenTappedAround()
}
@objc func a() {
print(">>>> pressed")
}
}
a
中的代码被调用。因此,我假设您的代码中还有其他原因导致按钮不起作用。也许您忘记了 link button
到 @IBAction
LoginBtnClick(_:)
- 检查连接是否真的存在。
我实际上以不同的方式解决了它(我在登录屏幕上遇到了同样的问题)。我只是将键盘的 dismissing/animating 移动到一个异步块中,延迟很小,因为它看起来像点击识别器,它正在拾取触摸,但 不是 按钮内部的修饰,可能是因为屏幕已经开始为键盘的关闭设置动画。
@objc func dismissKeyboard() {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
self.view.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
})
}
}
我在视图控制器的视图上添加了一个按钮(确保您的文本字段位于按钮前面)。
然后,我将 UITextFieldDelegate 实现到 enable/disable 按钮。
像这样:
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet private weak var myTextfield: UITextField!
@IBOutlet private weak var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.myTextfield.delegate = self
}
// MARK: - UITextFieldDelegate
func textFieldDidBeginEditing(_ textField: UITextField) {
self.myButton.isUserInteractionEnabled = true
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.myButton.isUserInteractionEnabled = false
}
// MARK: - Action functions.
@IBAction private func onButtonAction() {
self.myTextfield.resignFirstResponder()
}
}
在我的登录页面上,如果我专注于 UITextEdit 以输入电子邮件和密码,则会出现键盘并向上滚动登录页面。如果我触摸键盘外部,我会添加此代码以删除键盘。
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
})
}
}
在我的 viewDidLoad() 函数中,我是这样添加的。
override func viewDidLoad()
{
super.viewDidLoad()
//TODO
self.hideKeyboardWhenTappedAround()
...
}
它运行良好,但是如果我在键盘仍处于打开状态时单击登录按钮,dismisskeyboard() 功能有效但 btnClick 功能无效。
@IBAction func LoginBtnClick(_ sender: Any)
{
print("loginBtn")
//...
}
我该如何解决这个问题?
不要将手势添加到主视图 viewController 在 UIButton 下面添加一个全屏子视图并向其添加手势
我已经使用以下代码对其进行了测试:
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
})
}
}
class ViewController: UIViewController {
fileprivate let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 400, height: 50))
fileprivate let button = UIButton(frame: CGRect(x: 20, y: 200, width: 150, height: 50))
override func loadView() {
super.loadView()
view.addSubview(textField)
textField.backgroundColor = .lightGray
view.addSubview(button)
button.setTitle("Press me", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.addTarget(self, action: #selector(a), for: .touchUpInside)
hideKeyboardWhenTappedAround()
}
@objc func a() {
print(">>>> pressed")
}
}
a
中的代码被调用。因此,我假设您的代码中还有其他原因导致按钮不起作用。也许您忘记了 link button
到 @IBAction
LoginBtnClick(_:)
- 检查连接是否真的存在。
我实际上以不同的方式解决了它(我在登录屏幕上遇到了同样的问题)。我只是将键盘的 dismissing/animating 移动到一个异步块中,延迟很小,因为它看起来像点击识别器,它正在拾取触摸,但 不是 按钮内部的修饰,可能是因为屏幕已经开始为键盘的关闭设置动画。
@objc func dismissKeyboard() {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
self.view.endEditing(true)
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
})
}
}
我在视图控制器的视图上添加了一个按钮(确保您的文本字段位于按钮前面)。 然后,我将 UITextFieldDelegate 实现到 enable/disable 按钮。 像这样:
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet private weak var myTextfield: UITextField!
@IBOutlet private weak var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.myTextfield.delegate = self
}
// MARK: - UITextFieldDelegate
func textFieldDidBeginEditing(_ textField: UITextField) {
self.myButton.isUserInteractionEnabled = true
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.myButton.isUserInteractionEnabled = false
}
// MARK: - Action functions.
@IBAction private func onButtonAction() {
self.myTextfield.resignFirstResponder()
}
}