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

查看:546
本文介绍了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目录示例代码,包含一个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天全站免登陆