摘自苹果的开发者文档 here ,我遇到了这个。
Instead of handling a gesture, you could choose to track and handle the “raw” touches that make up the gesture.
文档中的示例是针对 Objective-C 给出的,但不是针对 Swift 的。我如何在全局范围内跟踪和处理 Swift 中的这些“原始”接触?不只是在 NSView 中?
我的类(class):
import Cocoa
import Security
import AppKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var dropMenu: NSMenu!
@IBOutlet weak var resetView: NSView!
@IBOutlet weak var resetWindow: NSPanel!
@IBOutlet weak var keyLabel: NSTextField!
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1);
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSApp.setActivationPolicy(NSApplicationActivationPolicy.Accessory)
let menuIcon = NSImage(named: "menuIcon")
menuIcon?.template = true;
statusItem.image = menuIcon;
statusItem.menu = dropMenu;
}
@IBAction func quit(sender: NSMenuItem) {
NSApplication.sharedApplication().terminate(self)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject! in touches {
let touchLocation = touch.locationInNode(self)
//Use touchLocation for example: button.containsPoint(touchLocation) meaning the user has pressed the button.
}
}
}
最佳答案
再次更新,NSEvent:
(touchesBegan UIEvent 等适用于 iOS,MacOS 不同并使用 NSEvent)
NSEvent 是您想要查看的内容,您可以在其中使用 mouseDown、mouseUp、mouseMove 等,然后获取光标点:
mouseDown 的 Swift 示例:
override func mouseDown(event: NSEvent) {
let point: NSPoint = event.locationInView
print("X: \'point.x'")
print("Y: \'point.y'")
}
对象示例:
- (void)mouseDown:(NSEvent *)event {
NSLog( @"mouse down event: %@", event );
NSPoint point = [event locationInWindow];
NSLog( @"mouseDown location: (%d,%d)", point.x, point.y );
}
希望这有帮助。
关于ios - 使用 Swift 处理触控板事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483386/