WatchKit API如何支持Force Touch / Digital Crown?

26

我对Apple Watch引入的新用户交互可能性,尤其是Force Touch和Digital Crown感到非常兴奋。

然而,在WatchKit API中我找不到它们的提及。有没有方法可以从Force Touch / Digital Crown接收事件?是否可以为这些事件自定义处理程序?


3
Force Touch仅仅显示菜单项,你可以在WKInterfaceControllers上使用addMenuItem...添加。force touches不会返回任何操作。数字冠也是如此。 - mattyohe
5个回答

14

watchOS 3 添加了 WKCrownSequencerWKCrownDelegate,以报告数字表冠的状态(例如旋转速度),并在用户旋转表冠时接收通知。

您可以使用表冠序列来提供一般输入以控制场景或界面对象。

苹果已更新其WatchKit目录示例代码,包括一个WKInterfaceController表冠序列示例,演示如何使用Apple Watch数字表冠与其他对象交互:

class CrownDetailController: WKInterfaceController, WKCrownDelegate {
    @IBOutlet var velocityLabel: WKInterfaceLabel!
    @IBOutlet var stateLabel: WKInterfaceLabel!
    @IBOutlet var pickerView: WKInterfacePicker!

    override func awake(withContext context: AnyObject?) {
        super.awake(withContext: context)

        let itemList: [(String, String)] = [
            ("Item 1", "Red"),
            ("Item 2", "Green"),
            ("Item 3", "Blue")
        ]
        let pickerItems: [WKPickerItem] = itemList.map {
            let pickerItem = WKPickerItem()
            pickerItem.caption = $0.0
            pickerItem.title = $0.1
            return pickerItem
        }
        pickerView.setItems(pickerItems)

        crownSequencer.delegate = self
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
        crownSequencer.focus()
    }

    @IBAction func focusCrown(sender: AnyObject) {
        crownSequencer.focus()
    }

    func updateCrownLabels() {
        velocityLabel.setText(String(format: "RPS: %2.2lf", crownSequencer.rotationsPerSecond))
        stateLabel.setText(crownSequencer.isIdle ? "Idle: true" : "Idle: false")
    }

    func crownDidBecomeIdle(_ crownSequencer: WKCrownSequencer?) {
        updateCrownLabels()
    }

    func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
        updateCrownLabels()
    }

}

12

目前(Xcode 6.2 beta 3),苹果表示Digital Crown可以在第三方应用中滚动页面内容,但是应用程序无法注册接收来自Digital Crown的通知或将其用作输入设备。这似乎很可能是由于担心延迟导致反应迟钝,或者考虑到电池寿命,因为第三方代码仅在iPhone上的WatchKit应用程序扩展中运行,因此这些事件必须通过无线流发送回手机。

在第三方应用中,可以使用Force Touch作为输入方式。在这一点上,我认为苹果建议的任何应用程序中唯一使用Force Touch的方法是显示上下文菜单,并且第三方开发人员完全可以访问此功能。您不需要注册即可接收Force Touch事件。您只需在界面生成器中创建菜单,向其中添加菜单项,然后将这些菜单项与WatchKit Extension中的IBActions连接即可。并没有将菜单链接到类似于force touch手势识别器的东西,这实际上相当奇怪,但是当您尝试它时,您会发现它确实有效。即使在2015年晚些时候我们有能力制作适用于Watch的本机应用程序之后,第三方开发人员可以使用的Force Touch可能仍然是这种方式。

然而,苹果鼓励开发人员在http://bugreporter.apple.com上提交增强请求,因此建议这样做,并要求访问这些API,因为我们中的一些人已经这样做了。


10

目前无法自定义输入方法的事件处理程序。苹果公司员工在开发者论坛上建议提交功能请求。我猜测,我们在明年开始创建原生的Apple Watch应用程序时将能够访问Digital Crown API。目前,这仍然是数据输入的重要限制。


7

在Xcode 7 beta中,watchOS 2(苹果公司推出的操作系统)增加了WKInterfacePicker。该控件允许您注册代理并响应数字表冠的变化。您可以在这里查看文档。


4
在WatchOS 2中,使用数字表冠来选择WKInterfacePicker中的项目:
在手表应用程序扩展InterfaceController.h中。
@property (strong, nonatomic) IBOutlet WKInterfacePicker *TestSelector;
@property (strong, nonatomic) NSArray *TestsArray;

在手表应用程序扩展InterfaceController.m中。
// Setup two items the user can select from in picker
WKPickerItem *pickerItem1 = [WKPickerItem alloc];
[pickerItem1 setTitle:@"First item in list"];

WKPickerItem *pickerItem2 = [WKPickerItem alloc];
[pickerItem2 setTitle:@"Second item in list"];

// Add items to array that will be used to set picker list
self.TestsArray = [[NSArray alloc ]  initWithObjects:pickerItem1,pickerItem2, nil];
// Set list of items as choices in the picker list
[TestSelector setItems:self.TestsArray];

在界面构建器中,将选择器 UI 元素链接到 TestSelector 作为引用出口。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接