【问题标题】:Detect mouse wheel -END- event on NSScrollView (AppKit)在 NSScrollView (AppKit) 上检测鼠标滚轮 -END- 事件
【发布时间】:2022-11-11 23:05:03
【问题描述】:

我有一种感觉,这是苹果的另一个“魔法”,
但我无法检测到非常简单和基本的事件: 当用户停止使用鼠标滚轮滚动时(-end- 事件)

- (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");
    }
    
}

我检查了 NSEvent 的所有属性,但没有要检查的数据。 谷歌搜索以检查其他人在做什么。没有什么能真正解决这个问题。 一些解决方案是这样的,但是......这总是返回 0。

【问题讨论】:

  • 据我所知,没有鼠标滚轮结束事件,就像没有鼠标停止移动事件或用户停止打字事件一样。 NSEvent.phase 与手势一起使用。
  • 但是有“用户停止滚动事件”(用鼠标移动滚动条时)。问题是如果用户使用鼠标滚轮滚动我也无法检测到它。所以没有它就没用了。
  • 滚动条检测到鼠标向上事件。应该如何检测到滚轮结束事件?

标签: cocoa appkit nsscrollview


【解决方案1】:

由具有传统滚轮的鼠标触发的滚轮事件是离散处理的。没有begin / end 阶段。一般来说,当NSEvent.momentumPhase为0时,产生滚轮事件的设备很可能是传统鼠标。

从触控板、Magic Mouse 或其他支持“手势”的设备生成的滚轮事件使用NSEvent.momentumPhase 报告滚动所处的阶段。对于这些事件,您可以使用NSEvent.momentumPhase 跟踪滚动开始时间和滚动结束时间.

一个视图可以选择性地发布它自己的begin / end 通知,这些通知与它将如何处理滚动有关,但这些通知不是处理滚动事件的NSEvent 机制的一部分。

根据您的用例,您最好只听NSView.boundsDidChangeNotification 以了解您的视图的视口何时更改,而不是尝试覆盖所有各种滚动触发器。

Apple 较早的 AppKit 发行说明中讨论了很多内容。可悲的是,它现在似乎只是一个大文档,但是如果您四处搜索,您会看到一些部分讨论如何正确处理滚轮事件以及它们多年来的演变:

AppKit Release Notes (macOS 10.12 and Earlier)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多