UIBarButtonItem Long Press / Short Press

删除回忆录丶 提交于 2019-12-25 00:51:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!