Swift Gesture识别器添加到textfield后无法运行成为第一响应者?

时间:2015-06-19 11:59:19

标签: swift uitextfield uigesturerecognizer tap

我在swift中为文本字段添加了一个点击和双击手势识别器,以便单击执行一个功能,但双击实际上使Textfield成为第一响应者。在文本字段第一次成为第一响应者之后,它停止工作。也就是说,文本字段通过在单击而不是双击时成为第一响应者而正常操作。关于如何让我的手势识别器工作的任何想法?

3 个答案:

答案 0 :(得分:3)

另一种方法:

只需创建一个'隐形'在文本字段顶部查看并向其添加手势识别器

    import UIKit

    class ViewController: UIViewController, UITextFieldDelegate {

        @IBOutlet weak var textField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            textField.delegate = self;
        }

        override func viewDidAppear(animated: Bool) {
            let tap1 = UITapGestureRecognizer(target: self, action: "onSingleTap")
            let tap2 = UITapGestureRecognizer(target: self, action: "onDoubleTap")
            tap2.numberOfTapsRequired = 2

            // create a view and put it on top of the textfield
            // here created at viewDidAppear for example but you can do it better

            var dummieView = UIView(frame: self.textField.frame)
            dummieView.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.25)
            self.view.addSubview(dummieView)
            dummieView.addGestureRecognizer(tap1)
            dummieView.addGestureRecognizer(tap2)
        }

        func onSingleTap(){
            println("custom single tap")
            if self.textField.isFirstResponder() {
                self.textField.resignFirstResponder()
            }
        }

        func onDoubleTap(){
            println("double tap")
            self.textField.becomeFirstResponder()
        }

        func textFieldShouldReturn(textField: UITextField) -> Bool {
            if self.textField.isFirstResponder() {
                self.textField.resignFirstResponder()
            }
            return true
        }


    }

答案 1 :(得分:1)

从逻辑上讲,如果您按照以下步骤操作会更容易:

  1. 将“editable”false设置为texField
  2. on doubleTap动作实现使文本域可编辑为 true并调用becomeFirstResponder以便用户可以键入。
  3. 实现textfield委托方法-textFieldDidEndEditing,并通过将“editable”属性设置为false再次使该textField不可编辑。

答案 2 :(得分:0)

我遇到了类似的问题,想要在显示和隐藏UIPicker之间切换一下。

问题在于UITextField添加了自己的手势识别器。因此,子类UITextField并添加您自己的,但禁止添加默认手势识别器。

class MyTextField: UITextField
{
    fileprivate var tap1Gesture: UITapGestureRecognizer?
    fileprivate var tap2Gesture: UITapGestureRecognizer?

    required init?( coder aDecoder: NSCoder )
    {
        super.init( coder: aDecoder )

        gestureRecognizers = nil
        tap1Gesture = UITapGestureRecognizer( target: self, action: #selector( tapped1( _: ) ) )
        addGestureRecognizer( tap1Gesture! )
        tap2Gesture = UITapGestureRecognizer( target: self, action: #selector( tapped2( _: ) ) )
        tap2Gesture.numberOfTapsRequired = 2
        addGestureRecognizer( tap2Gesture! )
    }

    override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer)
    {
        if gestureRecognizer == tap1Gesture || gestureRecognizer == tap2Gesture
        {
            super.addGestureRecognizer( gestureRecognizer )
        }
    }

    internal func tapped1( _ sender: UITapGestureRecognizer )
    {
        // Do your "performs a function"
    }

    internal func tapped2( _ sender: UITapGestureRecognizer )
    {
        if self.isFirstResponder
        {
            _ = self.resignFirstResponder()
        }
        else
        {
            self.becomeFirstResponder()
        }
    }
}

如果要隐藏文本选择,还可以将以下内容添加到派生类中(使用帮助http://www.b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/):

extension MyTextField
{
    override var selectedTextRange: UITextRange?
    {
        get { return nil }
        set { return }
    }

    override func canPerformAction( _ action: Selector, withSender sender: Any? ) -> Bool
    {
        return false
    }

    override func caretRect( for position: UITextPosition ) -> CGRect
    {
        return .zero
    }
}