在 cocoa 上处理鼠标事件时出错

Error on handling mouse events on cocoa

我有一个定制 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为!我的ViewController vc.mouseUp // 或 vc.mouseDown

--> 仍然出现上述错误

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

    NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown){
        self.mouseDown(with: [=13=])
        return [=13=] 
    }
    
    
    NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp){
        self.mouseUp(with: [=13=])
        return [=13=]
    }
    

--> 仍然出现上述错误

问题:如何让这两种方法在我的 ViewController 中工作?

重要提示:请考虑我使用的是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:) 的文档说:

This method is only valid for gesture events (gesture, magnify, swipe, rotate, etc.).