简体   繁体   English

在swift中使用UITextField的UILongPressGestureRecognizer

[英]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? 是否可以在UITextField上使用UILongPressGestureRecognizer而不触发字段编辑,同时仍能在常规点击中编辑文本字段?

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. 我曾尝试在UITextField中添加长按手势识别器,但长按似乎只能在很短的时间内完成。

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 创建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 将手势recognizer添加到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 现在你可以检查textFiledPressed(gesture:)功能,如果长按或不按

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. 这里这里看到类似的SO,并试验自己之后,我认为这是不可能的 - 至少你描述你的意图的方式。

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. FWIW,这是使用双击添加背景UIView的代码。 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: 这是故事板的图像:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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