ios - Apple Watch 中的手势识别 (WatchKit)

标签 ios objective-c watchkit apple-watch

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

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

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

最佳答案

watchOS 3 添加了对 WKGestureRecognizer 的第三方支持.

Apple 还更新了他们的 WatchKit Catalog sample code包括一个 WKInterfaceController 手势示例,演示如何在 Apple Watch 上使用各种手势。

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

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()
        }
    }

关于ios - Apple Watch 中的手势识别 (WatchKit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524733/

相关文章:

objective-c - UILabel 有触摸方法吗?

ios - 在 "Settings App"中打开辅助功能 -> 缩放时,三指 UIPanGestureRecognizer 不起作用

ios - 如果屏幕方向已更改,AppDelegate应调用该方法

ios - WatchKit 组 : with image and label, 图像的 4 个角中的 2 个为圆角

ios - WatchKit WKInterfaceGroup 可以是一个按钮吗?

ios - WatchKit 拉动刷新

ios - 简单的 KVO 例子

ios - 在 iOS 中使用模板缓冲区

objective-c - 按 NSDate 对 NSMutableArray 中的 NSDictionaries 进行排序

iphone - 裁剪和缩放 CGContext - iOS