Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a custom UIView to respond to a two finger trackpad scroll gesture (or mouse scroll wheel)

I have an iPad app (in C#) with a custom UIView that allows input via touch and Apple Pencil stylus touches. I am trying to integrate support for trackpad/mouse indirect (cursor, or "pointer" as Apple calls it).

I got hover working using HoverGestureRecognizer. I got right-click and control-click working using normal touch Began/Moved/Ended/Cancelled events and checking for .type == .indirectPointer and then checking if the control key modifier in event.ModifierFlags is set, or if event.ButtonMask == secondary.

I have spent a lot of time searching through the documentation on the Apple Developer website, starting here and branching out:

  • UIApplicationSupportsIndirectInputEvents

Somehow I cannot find the API that the system calls in my code when a two-finger trackpad scroll (or mouse scrollwheel scroll) occurs. (On another view that is a scrollview, I can get the scrollview's scroll event when I do a two-finger scroll, since this is built-in to iPadOS 13.4+ for scroll views, but my custom view is not a scroll view, it just has some scrollable areas inside of it.)

Things I tried:

  • UISwipeGestureRecognizer. Nothing was called for two-finger trackpad scroll gesture.
  • UIPanGestureRecognizer. Nothing.
  • Subclassing UIScrollView and adding a UIScrollViewDelegate, just to see if it would work... Nothing.
  • Subclassing GestureRecognizer and adding that, then overriding ShouldReceive(UIEvent evt) but that was never called.

What does iPadOS 13.4+ convert the trackpad two-finger scroll gesture into? Can I get this as some sort of event? The documentation linked above is pretty disappointingly barebones, but it mentions UIEvent.EventType.scroll but not how or when or where the system will call any of my methods with an event of that type. Pretty infuriating. They should just spell this out more clearly.

Answers in Swift or C# are welcomed.

like image 473
Jared Updike Avatar asked May 17 '20 00:05

Jared Updike


Video Answer


1 Answers

OK, strangely I thought I tried PanGestureRecognizer, but I must have set it up wrong. The example code project by Apple, Integrating Pointer Interactions into Your iPad App had the answer (C# code):

        panRecognizer = new UIPanGestureRecognizer(() => {
            Console.WriteLine("panned -- " + panRecognizer.VelocityInView(this));
        });
        panRecognizer.AllowedScrollTypesMask = UIScrollTypeMask.Continuous;
        AddGestureRecognizer(panRecognizer);

Glad I figured this out!

like image 65
Jared Updike Avatar answered Oct 20 '22 10:10

Jared Updike