如果我在swift中添加UITapGestureRecognizer,则UIButton Click Event无效

时间:2018-01-11 15:53:12

标签: ios swift uibutton uitapgesturerecognizer

在我的登录页面上,如果我专注于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")
        //...
}

我该如何解决这个问题? 请帮我。 谢谢。

4 个答案:

答案 0 :(得分:1)

我使用以下代码对其进行了测试:

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中的代码被调用。因此,我假设您的代码中还有其他内容导致按钮不起作用。也许您忘记将button@IBAction LoginBtnClick(_:)相关联 - 请检查连接是否确实存在。

答案 1 :(得分:0)

不要将手势添加到主viewController的视图中,在UIButton下添加全屏子视图并将手势添加到其中

答案 2 :(得分:0)

我实际上以不同的方式解决了它(我在登录屏幕上遇到了相同的问题)。我只是将消除/激活键盘的动作稍微延迟了一个异步块,因为在敲击识别器的作用下,它似乎在拾取触摸,但不是按钮,可能是因为屏幕已经开始为关闭键盘动画了。

@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)
        })
    }
}

答案 3 :(得分:0)

我在视图控制器的视图上添加了一个按钮(请确保您的文本字段位于该按钮的前面)。 然后,我实现了UITextFieldDelegate以启用/禁用按钮。 像这样:

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()
    }
}