处理可可上的鼠标事件时出错



>我有一个自定义ViewController,一个自定义WindowController和一个自定义ViewViewController连接到WindowControllerView连接到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
)

我已经尝试了以下方法:

  1. 在 WindowController 中注册这两个方法,然后只写入每个方法:

    让 vc = self.nextResponder as!我的视图控制器 vc.mouseUp//or vc.mouseDown

-->仍然收到上述错误

  1. 通过编写以下代码在viewDidLoad() / windowDidLoad()中注册这两个方法:

    NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown){
    self.mouseDown(with: $0)
    return $0 
    }
    
    NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp){
    self.mouseUp(with: $0)
    return $0
    }
    

-->仍然收到上述错误

问:如何使这两种方法在我的视图控制器中工作?

重要提示:请注意,我使用的是Swift 4.0.2和 Xcode9.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:)的文件说:

此方法仅对手势事件(手势、放大、轻扫、旋转等)有效。

相关内容

  • 没有找到相关文章

最新更新