问题
I have looked through the existing questions on this subject matter, and there appears to be no answers post iOS 11 (which appeared to break the gesturerecognizers).
Is there a way to detect the short press / long press on a UIBarButtonItem? Apple uses this functionality in Pages, Numbers, Keynote for Undo / Redo.
回答1:
Try this
@IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}
@objc func tap() {
print("Single tap done")
}
@objc func long() {
print("Long gesture recognized")
}
来源:https://stackoverflow.com/questions/54385142/uibarbuttonitem-long-press-short-press