用于 Force Touch/Digital Crown 的 WatchKit API? [英] WatchKit API for Force Touch / Digital Crown?

查看:15
本文介绍了用于 Force Touch/Digital Crown 的 WatchKit API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm very excited about the new user interaction possibilities introduced by the Apple Watch, among them, Force Touch and Digital Crown.

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

However, I couldn't find mentions of them in the WatchKit API. Are there any ways to receive events from Force Touch / Digital Crown? Is it possible to have custom handlers for the events?

推荐答案

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

watchOS 3 adds WKCrownSequencer and WKCrownDelegate to report the state of the digital crown (such as rotational speed), as well as to receive notifications when the user rotates the crown.

您可以使用冠音序器提供一般输入来控制场景或界面对象.

You can use the crown sequencer to provide general input to control scenes or interface objects.

Apple 更新了他们的 WatchKit Catalog 示例代码以包含一个WKInterfaceController 表冠音序器示例演示如何使用 Apple Watch 数字表冠与其他对象交互:

Apple has updated their WatchKit Catalog sample code to include a WKInterfaceController crown sequencer example demonstrating how to use the Apple Watch digital crown to interact with other objects:

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

}

这篇关于用于 Force Touch/Digital Crown 的 WatchKit API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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