UILongPressGestureRecognizer over UITextField in swift

Viewed 1612

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")
    }
}
2 Answers
Related