ios - 使用 Swift 处理触控板事件

标签 ios swift cocoa

摘自苹果的开发者文档 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 等,然后获取光标点:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/index.html#//apple_ref/occ/instm/NSResponder/mouseDown :

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/

相关文章:

iphone - 将应用程序提交到 itune connect 使用不同的默认语言然后是英语(未找到应用程序记录。)

ios - DeqeueReusableCellWithIdentifier 重叠两个单元格

ios - 在 TableViewController 中呈现嵌套数组

swift - UITableViewCell 函数

cocoa - 使用 NSApplicationDelegate 时无法使 Apple 的 SimpleScripting 示例正常工作

ios - 为什么 viewForAnnotation 方法弄乱了我的用户位置?

iOS : using activity indicator in loading for second view controller

image - 快速将图像保存到特殊相册

ios - 将 CATextLayer 字体转换为 UIFont

objective-c - 从 Cocoa 中的 URL 方案中捕获 URL