0

I'm facing an issue whereby UIPanGestureRecognizer selector is not being called. UIPanGestureRecognizer is being tied to a WKWebView and its delegate to self (view controller).

UIPanGestureRecognizer delegate method is successfully called but not the selector method.

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panningMethod:)];

[pan setDelegate:self];

[self.mapView addGestureRecognizer:pan];


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

Any idea? I ran the code in earlier iOS version (e.g. iOS 12.2) and it is working perfectly fine

Ol Sen
  • 3,163
  • 2
  • 21
  • 30
Meh Meh
  • 3
  • 1
  • it is very likely that you got the impression of a working source code in older iOS version because maybe it was behaving as expected but the @selector function was never called to handle panning events. Also your value is called mapView but you pan which view then? The WKWebView? – Ol Sen Jul 03 '20 at 08:52

3 Answers3

0

I have a few questions/guesses related to the above.

  1. Are you sure the delegate method is called for that pan gesture recognizer? Could you save your UIPanGestureRecognizer into the ivar and compare the addresses during the debug session? I mean the address of the ivar and of the gestures from your delegate method.
  2. I can't say for sure why, but I got a similar problem once when I didn't implement other delegate method: -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return YES; }
  3. If nothing else works - this solution could help you: https://stackoverflow.com/a/33819119/3008827

Hope this could help you. Sorry, if no.

Nahash
  • 320
  • 4
  • 12
0

WKWebView might be suppressing the gesture, found related discussion here: Can't handle touches simultaneously with WKWebView on iOS 13.4

Hope this could help you.

Shan
  • 41
  • 1
0

The WkWebView's loaded contents is responsible for the navigation inside, usually done with javascript or the default scrolling behaviour of webviews. While WKWebView is a inherited class from NSView you can do all stuff with it that is possible with the super class.

so you can call

[self.mapview resizeSubviewsWithOldSize:NSZeroSize]; 

or/and

- (BOOL)canBecomeFirstResponder {
  return YES;
}

can be set.

You could also explicit define how interaction is handled. The following example code was meant to be able to turn off a (bool)_cursor value to stop cursor interactions of a wkwebview.

#pragma mark - interaction interception

- (NSView*)hitTest:(NSPoint)point {
    if (_cursor) return [super hitTest:point];
    return nil;
}

- (void)keyDown:(NSEvent *)event {
    [super keyDown:event];
}
- (void)keyUp:(NSEvent *)event {
    [super keyUp:event];
}

-(void)mouseMoved:(NSEvent *)event {
    if (_cursor) [super mouseMoved:event];
}
- (void)mouseDown:(NSEvent *)event {
    if (_cursor) [super mouseDown:event];
}
- (void)mouseUp:(NSEvent *)event {
    if (_cursor) [super mouseUp:event];
}
- (void)mouseDragged:(NSEvent *)event {
    if (_cursor) [super mouseDragged:event];
}
- (void)mouseEntered:(NSEvent *)event {
    if (_cursor) [super mouseEntered:event];
}
- (void)mouseExited:(NSEvent *)event {
    if (_cursor) [super mouseExited:event];
}

But there is a much simpler way. You don't need UIPanGestureRecognizer when you implement the NSResponder protocol or use the corresponding methods for Views..

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // do stuff 
    [super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        // do stuff;
    }
    [super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        // do stuff;
    }
    [super touchesEnded:touches withEvent:event];
}

and last but not least.. just in case you missed the definition of your selector your WKWebView needs the following @implementation

-(void)panningMethod:(UIPanGestureRecognizer *)gesture {
}

and usually also the @interface definition

-(void)panningMethod:(UIPanGestureRecognizer *)gesture;

and finally you do not need to call the explicit -setDelegate method after definition of a GestureRecogniser unless you meant to set the -navigationDelegate for the WKWebView.

Ol Sen
  • 3,163
  • 2
  • 21
  • 30
  • and your `-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer`method is called and definied from/in where? – Ol Sen Jul 03 '20 at 08:45