Scroll Wheel event capture in a Catalyst-converted Mac app

Viewed 895

When working with a Mac app which was converted from iOS using Catalyst, the usual ways to capture a mouse's scroll wheel activity for Mac such as with

  • (void)scrollWheel:(NSEvent *)event;

do not work as NSEvent apparently is not supported when building a Catalyst converted app.

The object I need to control is in a regular image container and not in a scroll view container. I am simply trying to use the scroll wheel to change the loaded image. Trackpad activity works fine but capturing the scroll wheel so far has been elusive.

Thanks!

2 Answers

Use a UIPanGestureRecognizer with allowedScrollTypesMask set to UIScrollTypeMaskDiscrete:

    // pan gesture to recognize mouse-wheel scrolling (zoom)
    UIPanGestureRecognizer * scrollWheelGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScrollWheelGesture:)];
    scrollWheelGesture.allowedScrollTypesMask = UIScrollTypeMaskDiscrete;   // only accept scroll-wheel, not track-pad
    scrollWheelGesture.maximumNumberOfTouches = 0;
    [self.view addGestureRecognizer:scrollWheelGesture];

and then

- (void)handleScrollWheelGesture:(UIPanGestureRecognizer *)pan
{
    CGPoint delta = [pan translationInView:self.view];
    CGFloat zoom = (1000 + delta.y) / 1000;
    [self adjustZoomBy:zoom];
}

Swift 5:

func setupScrollWheel() {
    let scrollWheelGesture = UIPanGestureRecognizer(target: self, action: #selector(scrollWheelGestureRecognizer(_:)))
    scrollWheelGesture.allowedScrollTypesMask = .discrete
    scrollWheelGesture.maximumNumberOfTouches = 0
    view.addGestureRecognizer(scrollWheelGesture)
}

@objc func scrollWheelGestureRecognizer(_ recognizer: UIPanGestureRecognizer) {
    let delta = recognizer.translation(in: view)      
}
Related