0

I used long press gesture recognizer to set my own wkwebview link actionsheet, however the actionsheet won't appear until I release my finger (I tried recognizer.began but it does not work...)

So, how can I intercept the long press recognizer? Can I add a timer to stop it? How?

P.s. here's are my code:

//long press to show the action sheet
        longPressRecognizer.delegate = self
        longPressRecognizer.addTarget(self, action: "onLongPress:")
        self.webView.scrollView.addGestureRecognizer(longPressRecognizer)

func onLongPress(gestureRecognizer:UIGestureRecognizer){
        longPressSwitch = true
    }

1 Answers1

0

Instead of a long press recognizer, use an NSTimer. Here's a quick implementation example (Swift 2):

var sameTouch = false
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    _ = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: "longPressed", userInfo: nil, repeats: false)
    sameTouch = true
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    sameTouch = false
}

func longPressed() {
    if(sameTouch) {
        longPressSwitch = true
        sameTouch = false
    }
}

Pretty simple stuff; when 'touch began', we start the timer which will run for 1.5 seconds. We say _ = ... because we will never reference that timer again, so naming it is unnecessary. We also set the variable sameTouch to true.

If touchesEnded, we set sameTouch to false. This is because that would indicate to longPressed() that the touch was released early.

In longPressed(), we set longPressSwitch to true--that's from your code. The line, sameTouch = false is not 100% necessary, but it illustrates the closure of the system in my eyes.

Hope, this helps! If you have questions, please, tell me!

Daniel
  • 3,188
  • 14
  • 34
  • Doesn't work, because Wkwebview cannot recognise touchesBegan & touchesEnded events... and this is why I used UIGestureRecognizer. – Jason Honcheung Wong Feb 20 '16 at 09:08
  • @JasonHoncheungWong, are you using UIWebView or UIWebViewController? – Daniel Feb 20 '16 at 15:55
  • I used WKWebView instead of UIWebView... still struggling of this issue... or are there any method that could send a TouchUpInside event (make fake finger releasing signal) to the gesture recognizer? – Jason Honcheung Wong Feb 20 '16 at 16:52
  • @JasonHoncheungWong, if you want an action to run when touch is released, maybe add a UITapGestureRecognizer? If a UILongPressGestureRecognizer is being called, then a UITapGestureRecognizer should too as they are both subclasses of UIGestureRecognizer – Daniel Feb 20 '16 at 17:30
  • UITapGestureRecognizer is not being called while wkwebview was implemented.. Can you please take a look at [this](http://stackoverflow.com/questions/35499976/wkwebview-decidepolicyfornavigationaction-being-call-after-the-long-press-recogn) which I was stuck in... @Dopapp – Jason Honcheung Wong Feb 21 '16 at 13:16
  • @JasonHoncheungWong, so, just to be clear, do you want something to happen when a tap happens anywhere in the bounds of the web view, or do you want actions for links or something more specific like that? – Daniel Feb 21 '16 at 15:00
  • I want actions for links. I want to get the links url address before the action sheet menu shows up. – Jason Honcheung Wong Feb 21 '16 at 15:04