Handling Mouse Scroll Wheel events using UIPanGestureRecognizer

I'm trying to handle a mouse scroll wheel in one of my views (to invoke my zoomIn/zoomOut methods) using a UIPanGestureRecognizer with:

UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:myView action:@selector(handlePanGesture:)];

panRecognizer.allowedScrollTypesMask = UIScrollTypeMaskDiscrete;
[myView addGestureRecognizer:panRecognizer];

My handlePanGesture method is successfully called twice (once for UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded), but... then what do I do?

The passed UIPanGestureRecognizer* has numberOfTouches = nil, and I can't get any further info about the scroll wheel UIEvent from UIGestureRecognizer or its superclass.

How to I tell if it was a scroll up, or a scroll down?

See: wwdc20-10094

Replies

Here's some code I'm using. I grabbed it from somewhere and didn't make note of where it came from.

UIPanGestureRecognizer *scrollWheelGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScrollWheelGesture:)]; scrollWheelGesture.allowedScrollTypesMask = UIScrollTypeMaskDiscrete; // only accept scroll-wheel, not track-pad scrollWheelGesture.maximumNumberOfTouches = 0;

  • (void)handleScrollWheelGesture:(UIPanGestureRecognizer *)pan

{ CGPoint delta = [pan translationInView:self.view]; // delta.y will be negative if scrolling up and positive if down I think..