【问题标题】:How to get NSScrollView to scroll horizontally with the scroll wheel without the Shift key?如何让 NSScrollView 在没有 Shift 键的情况下使用滚轮水平滚动?
【发布时间】:2021-01-05 01:33:44
【问题描述】:

我正在使用NSScrollView 来实现一个标签栏,当有太多标签无法放入窗口时,它会水平滚动。我可以使用触控板左右移动滚动视图。我想让鼠标用户使用滚轮左右滚动。现在,如果用户在转动滚轮时按住 Shift 键,这已经可以工作了——选项卡将水平滚动。但是,我希望它在没有 Shift 键的情况下工作。例如,这就是 Safari 的选项卡的工作方式。

如何在不使用 Shift 键的情况下使用滚轮实现水平滚动?

请注意,我的标签栏不显示水平滚动条。由于 macOS 10.9 引入了响应式滚动,我不相信我们可以再覆盖 -scrollWheel:(请参阅本文底部的更新:https://stackoverflow.com/a/31201614/111418

【问题讨论】:

    标签: cocoa mousewheel nsscrollview


    【解决方案1】:

    我使用NSEvent 监视器来完成此操作。

    _scrollWheelEventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskScrollWheel handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
        NSPoint location = [_scrollView convertPoint:event.locationInWindow fromView:nil];
        // We want events:
        //   where the mouse is over the _scrollView
        //   and where the user is not modifying it with the SHIFT key
        //   and initiated by the scroll wheel and not the trackpad
        if ([_scrollView mouse:location inRect:_scrollView.bounds]
            && !event.modifierFlags
            && !event.hasPreciseScrollingDeltas)
        {
            // Create a new scroll wheel event based on the original,
            // but set the new deltaX to the original's deltaY.
            // stackoverflow.com/a/38991946/111418
            CGEventRef cgEvent = CGEventCreateCopy(event.CGEvent);
            CGEventSetIntegerValueField(cgEvent, kCGScrollWheelEventDeltaAxis2, event.scrollingDeltaY);
            NSEvent *newEvent = [NSEvent eventWithCGEvent:cgEvent];
            CFRelease(cgEvent);
            return newEvent;
        }
        return event;
    }];
    

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多