osx - NSPressGestureRecognizer called before minimumPressDuration -
i need nsbutton
respond regular clicks, long presses. adding nspressgesturerecognizer
so:
override func viewdidload() { super.viewdidload() let gr = nspressgesturerecognizer() gr.minimumpressduration = 1 gr.action = #selector(handlelongpress) button.addgesturerecognizer(gr) } func handlelongpress(gr: nspressgesturerecognizer) { if gr.state == .began { swift.print("long press") } }
unfortunately, handlelongpress
randomly fires @ short single clicks, or double clicks. happens if set minimumpressduration
higher values.
i have tried playing shouldberequiredtofailbygesturerecognizer
not solving problem.
is there missing code?
i have solved handling multiple gesture recognizers:
the class must implement nsgesturerecognizerdelegate
var singleclick:nsclickgesturerecognizer? var longclick:nspressgesturerecognizer? override func viewdidload() { super.viewdidload() longclick = nspressgesturerecognizer() longclick!.minimumpressduration = 1 longclick!.action = #selector(handlegesturelong) longclick!.delegate = self singleclick = nsclickgesturerecognizer() singleclick?.numberofclicksrequired = 1 singleclick!.action = #selector(handlegesturesingle) singleclick!.delegate = self btn.addgesturerecognizer(longclick!) btn.addgesturerecognizer(singleclick!) } func gesturerecognizer(gesturerecognizer: nsgesturerecognizer, shouldrequirefailureofgesturerecognizer othergesturerecognizer: nsgesturerecognizer) -> bool { if gesturerecognizer == singleclick && othergesturerecognizer == longclick { return false } return true } func handlegesturesingle(gr: nsclickgesturerecognizer) { switch gr.state { case .ended: print("single click") break default: break } } func handlegesturelong(gr: nspressgesturerecognizer) { switch gr.state { case .began: print("long click") break default: break } }
Comments
Post a Comment