0

我正在重写 NSResponder 的mouseEntered(theEvent: NSEvent)方法,并想检索我在 Swift 中创建 NSTrackingArea 时设置的值。我正在使用构造函数来创建 NSTrackingArea 并将以下对象作为userInfo.

let trackerData = ["myTrackerKey": view]
let trackingArea = NSTrackingArea(rect: trackingRect, options: [.EnabledDuringMouseDrag, .MouseEnteredAndExited, .ActiveInActiveApp], owner: self, userInfo: trackerData)

view : NSView以及trackingRect : NSRect确实存在)

我正在设置trackerDatauserInfo以便稍后在mouseEntered(theEvent: NSEvent).

override func mouseEntered(theEvent: NSEvent) {
    // This does not work
    let data1 = theEvent.userData as [NSObject : AnyObject]
    let data2 = theEvent.userData as [String : NSView]
    let data3 = theEvent.userData as NSDictionary
}

我的代码基于Apple 提供的这个Objective-C 示例代码。引用第 372 行SuggestionsWindowController.m

HighlightingView *view = [(NSDictionary*)[event userData] objectForKey: kTrackerKey];

那么我怎样才能阅读事件的userData

我看过这个 StackOverflow Q&A但它不起作用。

在此处输入图像描述

4

1 回答 1

1

如果trackerData要从事件中读取,则必须从跟踪区域获取用户信息

if let userInfo = theEvent.trackingArea?.userInfo {
  let trackerData = userInfo["myTrackerKey"]! as! NSView
  // do something with the view
}
于 2015-07-19T18:35:50.633 回答