如何将滚轮:处理程序更改为放大事件:调用



第一篇文章在这里!

我有一个NSScrollView子类。我希望鼠标的选项滚轮事件导致与触控板捏合缩放相同的缩放。我尝试了下面的简单解决方案,但这只会导致很多断言,可能是因为滚动事件的类型错误,并且没有放大事件所需的数据。我没有看到用于创建放大事件的方便的 NSEvent 类方法,例如它们为鼠标、键、回车/退出和其他事件提供的方法。

-(void) scrollWheel:(NSEvent*)event
{
    if(event.modifierFlags & NSAlternateKeyMask)
        [self magnifyWithEvent:event];
    else
        [super scrollWheel:event];
}

错误输出:

2018-01-18 11:35:59.509 BoF[71240:2227373] -_continuousScroll is deprecated for NSScrollWheel. Please use -hasPreciseScrollingDeltas.
2018-01-18 11:35:59.509 BoF[71240:2227373] -deviceDeltaX is deprecated for NSScrollWheel. Please use -scrollingDeltaX.
2018-01-18 11:35:59.509 BoF[71240:2227373] -deviceDeltaY is deprecated for NSScrollWheel. Please use -scrollingDeltaY.
2018-01-18 11:35:59.509 BoF[71240:2227373] *** Assertion failure in -[NSEvent magnification], /SourceCache/AppKit/AppKit-1348.17/AppKit.subproj/NSEvent.m:1890
2018-01-18 11:35:59.509 BoF[71240:2227373] Invalid message sent to event "NSEvent: type=ScrollWheel loc=(371,197) time=80177.1 flags=0x80120 win=0x6180001f8000 winNum=3959 ctxt=0x0 deltaX=0.000000 deltaY=0.400024 deltaZ=0.000000 deviceDeltaX=0.000000 deviceDeltaY=3.000000 deviceDeltaZ=0.000000 count:0 phase=Changed momentumPhase=None"
2018-01-18 11:35:59.643 BoF[71240:2227373] (
    0   CoreFoundation                      0x00007fff90c6e03c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8282176e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff90c6de1a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff8632897b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   AppKit                              0x00007fff8f96c8c2 -[NSEvent magnification] + 182
    5   AppKit                              0x00007fff8f96c488 -[NSScrollView magnifyWithEvent:] + 395
    6   BoF                      0x000000010002ceb3 -[ComicScrollView scrollWheel:] + 99
    7   AppKit                              0x00007fff8f7c4cfb forwardMethod + 126
    8   AppKit                              0x00007fff8f8f5d00 -[NSView scrollWheel:] + 507
    9   AppKit                              0x00007fff8f7c4cfb forwardMethod + 126
    10  AppKit                              0x00007fff8f8f5d00 -[NSView scrollWheel:] + 507
    11  AppKit                              0x00007fff8fe3b6cc -[NSWindow _reallySendEvent:isDelayedEvent:] + 6941
    12  AppKit                              0x00007fff8f7ccc86 -[NSWindow sendEvent:] + 470
    13  AppKit                              0x00007fff8f7c9137 -[NSApplication sendEvent:] + 2285
    14  AppKit                              0x00007fff8f6f2b68 -[NSApplication run] + 711
    15  AppKit                              0x00007fff8f66f244 NSApplicationMain + 1832
    16  BoF                      0x000000010000f232 main + 34
    17  libdyld.dylib                       0x00007fff848f95c9 start + 1
    18  ???                                 0x0000000000000003 0x0 + 3
)

好吧,它没有使用标准的 magnifyWithEvent: 代码路径,但它可以像这样伪造它,对乘数做出假设以将滚动量转换为放大倍数。

-(void) scrollWheel:(NSEvent*)event
{
    if(event.modifierFlags & NSAlternateKeyMask) {
        NSPoint         pt = [self.documentView convertPoint:event.locationInWindow fromView:nil];
        CGFloat         by = event.scrollingDeltaY * 0.001; // The smallest pinch-zoom amount seems to be about 0.002, but that was a bit too coarse.
        if(!event.hasPreciseScrollingDeltas)
            by *= self.verticalLineScroll;
        [self setMagnification:self.magnification + by centeredAtPoint:pt];
    }
    else
        [super scrollWheel:event];
}

一个可能的 Swift 5 版本,最好使用二次放大:

override func scrollWheel(with event: NSEvent) {
    if !event.momentumPhase.isEmpty || !event.phase.isEmpty {
        // magic trackpad or magic mouse
        super.scrollWheel(with: event)
    } else if event.modifierFlags.contains(.option) {
        // traditional mouse
        if let centerPoint = documentView?.convert(event.locationInWindow, from: nil) {
            let linearVal = CGFloat(log2(magnification))
            var linearDeltaY = event.scrollingDeltaY * 0.01
            if !event.hasPreciseScrollingDeltas {
                linearDeltaY *= verticalLineScroll
            }
            setMagnification(CGFloat(pow(2, linearVal + linearDeltaY)), centeredAt: centerPoint)
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新