gpt4 book ai didi

objective-c - 如何更改scrollWheel : handler into a magnifyWithEvent: call?

转载 作者:行者123 更新时间:2023-12-03 17:37:48 28 4
gpt4 key购买 nike

这里是第一篇文章!

我有一个 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];
}

关于objective-c - 如何更改scrollWheel : handler into a magnifyWithEvent: call?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327784/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com