【问题标题】:UILongPressGestureRecognizer over UITextField in swiftswift中UITextField上的UILongPressGestureRecognizer
【发布时间】:2017-12-08 05:04:38
【问题描述】:

是否可以在 UITextField 上使用 UILongPressGestureRecognizer 而不触发字段编辑,同时仍然能够在常规点击时编辑文本字段?

我尝试向 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")
    }
}

【问题讨论】:

  • 你想对 UILongPressGestureRecognizer 上的 UITextField 做什么?
  • @Maddyヅヅ 我想让一个按钮出现在 UITextField 旁边只长按
  • 可能默认的UIPressGestureRecognizer 与您的UILongPressGestureRecognizer 冲突。我认为你可以改变行为,例如,当这个UITextField 被聚焦时显示按钮。
  • 无论如何,我从未见过有这种行为的应用程序:长按文本字段时显示隐藏按钮。
  • @Macabeus 我也在考虑在顶部放置一个透明层 - 唯一的问题是我只想处理长按的情况,同时让其余的手势通过层在它后面。不确定如何停止传播是否发生了特定类型的手势。

标签: ios iphone swift uitextfield uigesturerecognizer


【解决方案1】:

创建 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
    }
}

将手势recognizer 添加到您的textField

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

现在你可以检查textFiledPressed(gesture:)功能是否长按

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

    default: break
    }

}

【讨论】:

  • 绝妙的解决方案!非常感谢!
【解决方案2】:

在查看了类似的 SO herehere 并进行了自己的实验之后,我认为这是不可能的 - 至少按照您描述意图的方式。

我能够添加带有手势控制的背景视图,但手势与文本字段上的用户交互冲突。文本字段旁边的按钮不会创造更好的用户体验吗?

FWIW,这是使用双击添加背景UIView 的代码。长按手势也有同样的结果

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

这是故事板的图片:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多