简体   繁体   中英

UILongPressGestureRecognizer over UITextField in swift

Is there away to use a UILongPressGestureRecognizer over a UITextField without triggering field edit while still being able to edit the textfield on a regular tap?

I have tried adding a long press gesture recognizer to the UITextField but the long press seems to only work a fraction of the time.

init(frame: CGRect, userCompany: WLUserCompany) {
    super.init(frame: frame)

    var textField: UITextField?

    var longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(gesture:)))
    textField?.addGestureRecognizer(longPress)

    self.addSubview(textField!)
}


@objc func longPress(gesture: UILongPressGestureRecognizer) {            
    if gesture.state == UIGestureRecognizerState.began {
        print("Long Press")
    }
}

Create subclass of UIGestureRecognizer

import UIKit.UIGestureRecognizerSubclass

class TouchGestureRecognizer: UIGestureRecognizer {

    var isLongPress: Bool = false
    fileprivate var startDateInterval: TimeInterval = 0

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
        state = .began
        self.startDateInterval = Date().timeIntervalSince1970
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesEnded(touches, with: event)
        state = .ended
        self.isLongPress = (Date().timeIntervalSince1970 - self.startDateInterval) > 1.0
    }
}

Add gesture recognizer to your textField

let gesture = TouchGestureRecognizer(target: self, action: #selector(ViewController.textFiledPressed(gesture:)))
        self.textField?.addGestureRecognizer(gesture)

And now you can check in textFiledPressed(gesture:) function if it's long press or not

func textFiledPressed(gesture: TouchGestureRecognizer) {
    switch gesture.state {
    case .ended:
        if gesture.isLongPress {
            //Do whatever you need
        } else {
            self.textField?.becomeFirstResponder()
        }

    default: break
    }

}

After looking at similar SO here and here , and experimenting myself, I don't think this is possible -- at least the way you describe your intent.

I was able to add a background view with gesture control, but the gesture conflicts with the user interaction on the text field. Wouldn't a button to the side of the textfield create a better user experience?

FWIW, here is the code to add a background UIView using a double tap. a long press gesture had the same result

 @IBOutlet weak var backgroundView: UIView!
    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.myTextFieldTapped))
        tapGesture.numberOfTapsRequired = 2
        backgroundView.addGestureRecognizer(tapGesture)
    }

    @objc func myTextFieldTapped() {
        print("Double tapped on textField")
    }

Here is an image of the storyboard:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM