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

非 Y 不嫁゛ 提交于 2020-08-04 23:14:09

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/61845476/getting-a-custom-uiview-to-respond-to-a-two-finger-trackpad-scroll-gesture-or-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!