我在 Xcode 中做了一个简单的项目来跟踪触控板上触摸事件的位置。我在 MainWindowController.xib 上为 TrackpadController 类创建了一个自定义视图实例:
class TrackpadController: NSView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect);
self.acceptsTouchEvents = true
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func touchesBeganWithEvent(event: NSEvent) {
let touches = event.touchesMatchingPhase(.Began, inView: nil)
for touch in touches {
let touchPosition = touch.normalizedPosition
Swift.print(touchPosition)
}
}
}
坐标会打印在控制台中,但前提是光标位于视图上方。当应用程序没有聚焦时,我希望能够跟踪触控板上的位置。任何关于如何获得这个的想法都会很棒。谢谢。