我有一个自定义ViewController
,自定义WindowController
和自定义View
。 ViewController
与WindowController
相连,View
与ViewController
相连。
在我的ViewController
中,我有以下代码用于鼠标事件:
override func mouseDown(with event: NSEvent) {
//code
}
override func mouseUp(with event: NSEvent) {
//code
}
在启动它时,我收到以下错误消息,而且调试这些方法甚至没有输入:
2018-03-08 11:27:11.594158+0100 MyProject[2697:83761] *** Assertion failure in -[NSEvent touchesMatchingPhase:inView:], /Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.83.101/AppKit.subproj/NSEvent.m:4654
2018-03-08 11:27:11.594574+0100 MyProject[2697:83761] [General] Invalid message sent to event "NSEvent: type=LMouseDown loc=(701.219,422.156) time=8788.4 flags=0 win=0x6080001e0200 winNum=1255 ctxt=0x0 evNum=1071 click=1 buttonNumber=0 pressure=1 deviceID:0x300000014400000 subtype=NSEventSubtypeTouch"
2018-03-08 11:27:11.599387+0100 MyProject[2697:83761] [General] (
0 CoreFoundation 0x00007fff8b59d2cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffa03b548d objc_exception_throw + 48
2 CoreFoundation 0x00007fff8b5a2042 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fff8cfeabe0 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 AppKit 0x00007fff89327573 -[NSEvent touchesMatchingPhase:inView:] + 205
5 MyProject 0x0000000100013c0b _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tF + 139
6 MyProject 0x0000000100014bbc _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tFTo + 60
7 AppKit 0x00007fff891897a9 forwardMethod + 133
8 AppKit 0x00007fff891897a9 forwardMethod + 133
9 AppKit 0x00007fff8990424f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
10 AppKit 0x00007fff89900a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
11 AppKit 0x00007fff898fff0a -[NSWindow(NSEventRouting) sendEvent:] + 541
12 AppKit 0x00007fff89784681 -[NSApplication(NSEvent) sendEvent:] + 1145
13 AppKit 0x00007fff88fff427 -[NSApplication run] + 1002
14 AppKit 0x00007fff88fc9e0e NSApplicationMain + 1237
15 MyProject 0x0000000100023add main + 13
16 libdyld.dylib 0x00007fffa0c9b235 start + 1
)
我已经尝试过以下的事情了:
- >仍然出现上述错误
viewDidLoad() / windowDidLoad()
中注册这两种方法:
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown){
self.mouseDown(with: $0)
return $0
}
NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp){
self.mouseUp(with: $0)
return $0
}
- >仍然出现上述错误
问题:如何使这两种方法在我的ViewController中工作?
重要说明:请考虑我使用的是Swift 4.0.2
和Xcode 9.1 Build 9B55
错误:
Assertion failure in -[NSEvent touchesMatchingPhase:inView:]
Invalid message sent to event "NSEvent: type=LMouseDown
touches(matching:in:)
来自mouseDown(with:)
。 touches(matching:in:)
的文档说:
此方法仅对手势事件(手势,放大,滑动,旋转等)有效。