Apple Watch (WatchKit) 中的手势识别 [英] Gesture recognition in Apple Watch (WatchKit)

查看:25
本文介绍了Apple Watch (WatchKit) 中的手势识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何通过 WatchKit SDK 在 Apple Watch 应用中检测手势.在 iOS 中,我们可以使用这样的代码:

I'm looking for how to detect gestures in an Apple Watch app, via the WatchKit SDK. In iOS we can use some code like this:

- (void)viewDidLoad
{
    ...
    UISwipeGestureRecognizer *swipeRightBlack = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)];
    swipeRightBlack.direction = UISwipeGestureRecognizerDirectionRight;
    [self.viewBlack addGestureRecognizer:swipeRightBlack];
}

...但这在 Apple Watch 模拟器中不起作用.有没有办法使用 WatchKit 覆盖默认的手势操作,或者在操作系统接收到它们时识别它们?

...but this doesn't work in the Apple Watch simulator. Is there a way to override the default gesture actions using WatchKit, or just recognize them when the OS receives them?

推荐答案

watchOS 3 为 WKGestureRecognizer.

watchOS 3 adds third-party support for WKGestureRecognizer.

Apple 还更新了他们的 WatchKit Catalog 示例代码 包含一个 WKInterfaceController 手势示例,演示如何在 Apple Watch 上使用各种手势.

Apple has also updated their WatchKit Catalog sample code to include a WKInterfaceController gesture example demonstrating how to use various gestures on the Apple Watch.

这是他们的代码片段,显示了 Storyboard 中配置的两个手势操作:

Here's a snippet from their code showing two of the actions for gestures configured in Storyboard:

class GestureDetailController : WKInterfaceController {
    @IBOutlet var tapGroup: WKInterfaceGroup!
    @IBOutlet var panGroup: WKInterfaceGroup!

    ...

    @IBAction func tapRecognized(_ sender: AnyObject) {
        tapGroup.setBackgroundColor(UIColor.green())
        scheduleReset()
    }

    @IBAction func panRecognized(_ sender: AnyObject) {
        if let panGesture = sender as? WKPanGestureRecognizer {
            panGroup.setBackgroundColor(UIColor.green())
            panLabel.setText("offset: \(NSStringFromCGPoint(panGesture.translationInObject()))")
            scheduleReset()
        }
    }

这篇关于Apple Watch (WatchKit) 中的手势识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆