My app uses an NSPageController to navigate forwards and backwards to several views with a swipe gesture.For some of this views, I am using a WKWebView. Unfortunately The main content view cannot capture the swipe gestures of its subview, becuase of a conflicht with the scroll behaviour of the webView. I tried to subclass the WKWebView, but with no success. How could I capture the swipe events? I thought about the NSTrackingArea, but ony mouse events are captures for this cases. Could you help to resolve this issue?
How to capture scroll wheel events of a subview?
I have opened a support ticket, but the answer is dissappointing:
Thank you for contacting Apple Developer Technical Support (DTS). Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.
If you would like for Apple to consider adding support for such features in the future, please submit an enhancement request via the Bug Reporter tool
Thus I still need a solution for the followiing problem: How can a superview capture scroll events of a subview?
Is ist possible to register for any events? What are possible workarounds? The most convenient way to show html documents with included referenced images is a WKWebView. Shoud I use another WebView? Or should I tinker a custom NSTextView? It was no problem to set an NSTextView as a DocumentView of an custom subclassed NSScrollView. In this custom NSScrollView I am able to capture the scroll event and forward it to the ContentView of the NSWindow to enable NSPageController navigations.
Ok, here is the solutio: I use WebView instead of WKWebView. And instead of injecting a script, that is not possible for WebView, I modify the html source, that is loaded. Furthermore I can create a custom class MyWebView and capture and forward the scroll event.
@implementation MyWebView
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
-(void)scrollWheel:(NSEvent *)theEvent {
// DDLogDebug(@"Scroll Event: %@", theEvent);
[super scrollWheel:theEvent];
[self.window.contentView scrollWheel:theEvent];
}
@end
You got it right! Still it would better to override it like this:
- (void)scrollWheel:(NSEvent *)event
{
[self.enclosingScrollView scrollWheel:event];
}
as it is NSResponder method there is no need to call super and you pass scrolling event to the closest enclosing NSScrollView instead of to some unknown contentView. Have a great day!