Home > Mobile >  Detect mouse wheel -END- event on NSScrollView (AppKit)
Detect mouse wheel -END- event on NSScrollView (AppKit)

Time:10-22

I have a feeling it's another Apple's "magic",
but I can't detect very simple and basic event : when user stop using mouse wheel to scroll (-end- event)

- (void)scrollWheel:(NSEvent *)event
{
    [super scrollWheel:event];
    
   
    ////    NSEventPhaseNone        = 0, // event not associated with a phase.
    ////    NSEventPhaseBegan       = 0x1 << 0,
    ////    NSEventPhaseStationary  = 0x1 << 1,
    ////    NSEventPhaseChanged     = 0x1 << 2,
    ////    NSEventPhaseEnded       = 0x1 << 3,
    ////    NSEventPhaseCancelled   = 0x1 << 4,
    ////    NSEventPhaseMayBegin    = 0x1 << 5,
    ///
    ///
    //event.phase -always- 0
    //event.momentumPhase -always- 0
    NSLog(@"event.momentumPhase = %lu, event.phase = %lu", (unsigned long)event.momentumPhase, event.phase);
    
    if(event.phase & NSEventPhaseBegan)
    {
        // never called when user start scrolling with mouse wheel
        NSLog(@"NSEventPhaseBegan");
    }
    if(event.phase & NSEventPhaseEnded)
    {
        // never called when user end scrolling with mouse wheel
        NSLog(@"NSEventPhaseEnded");
    }
    
}

I checked all the properties of the NSEvent, but there is no data to check. Googled to check what others are doing. nothing that really solve this. some solutions was this, but...this return 0 all the time.

CodePudding user response:

Scroll wheel events that are triggered by a mouse with a traditional scroll wheel are handled discretely. There are no begin / end phases. Generally speaking, when NSEvent.momentumPhase is 0, then the device generating the scroll wheel event is likely a traditional mouse.

A scroll wheel event generated from a trackpad, Magic Mouse or other device that supports "gestures" uses NSEvent.momentumPhase to report which phase the scrolling is in. For these events, you can track when scrolling starts and when scrolling ends using NSEvent.momentumPhase.

A view can, optionally, post its own begin / end notifications that are relevant to how it will handle scrolling, but those notifications are not part of the NSEvent mechanism for handling scrolling events.

Depending on your use-case, you might be better off just listening for NSView.boundsDidChangeNotification to know when the viewport of your view has changed, rather than trying to override all of the various scrolling triggers.

A lot of this is discussed in Apple's older release notes for AppKit. Sadly, it appears to be just one large document now but if you search around you'll see a few sections discussing how to properly handle scroll wheel events and how they have evolved over the years:

AppKit Release Notes (macOS 10.12 and Earlier)

  • Related