使用表冠控制 WKInterfaceSlider

Using watch crown to control a WKInterfaceSlider

我想用apple watch表冠来控制滑块。这可能吗?

如果是,怎么做?

苹果用它来改变手表 UI 的颜色。

编辑: 所以目前似乎不可能(见下面的答案)。重要的是要注意,在两周后(Apple WWDC 2015)这可能会改变(也许是独立应用程序的手表 OS?)

不可能。您无法控制表冠 – 它仅自动用于滚动等。

A​​pple 指南:

"Scrolling is the only supported Digital Crown interaction for apps, and the system automatically manages those interactions for you. Apps do not have direct access to the Digital Crown."

https://developer.apple.com/watch/human-interface-guidelines/

A​​pple 可能会在 Apple Watch 原生应用程序推出时启用数字表冠(我们将在 2 周后的 WWDC 上看到)。

如ps4所说,这根本不可能。

正如 Apple 在 Watch 设计指南中所述:

"Scrolling is the only supported Digital Crown interaction for apps, and the system automatically manages those interactions for you. Apps do not have direct access to the Digital Crown."

https://developer.apple.com/watch/human-interface-guidelines/

目前,这是不可能的。

但是,正如 5 月 27 日宣布的那样,Apple 将于 6 月在 WWDC 上推出适用于原生手表应用程序的 SDK。这将允许访问数字表冠。

来源:http://9to5mac.com/2015/05/27/live-blog-apple-senior-vp-of-operations-jeff-williams-interview-at-code-conference/

从 watchOS 2 beta 开始,您可以将数字表冠与 WKInterfacePicker 一起使用!

为了将数字表冠与 WKInterfaceSlider 结合使用,您需要做一些解决方法:

  • 将 WKInterfacePicker 添加到您的界面控制器并将其高度设置为 0,以便对用户隐藏它(如果将 'hidden' 设置为 true,则无法访问)
  • 生成一个包含滑块步数的数组,示例:

    items = [WKPickerItem](count: numberOfSteps, repeatedValue: WKPickerItem())
    picker.setItems(items)
  • 调用picker.focus()以接收数字表冠输入

  • 向选择器添加一个设置滑块值的操作,例如:

    @IBAction func pickerChanged(value: Int) {
        slider.setValue(Float(value + 1))
    }

所有答案都已过时。

在 watchOS 3 中,Apple 引入了 WKCrownSequencer class, that allows to track the user's interaction with the digital crown. You should implement the WKCrownDelegate 来通知旋转变化,然后将旋转角度映射到值的变化。以下是如何借助皇冠控制 WKInterfaceSlider 的示例:

class SliderInterfaceController: WKInterfaceController {
    @IBOutlet var slider: WKInterfaceSlider!

    let minVal: Float = 0
    let maxVal: Float = 10

    var selectedVal: Float = 0

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

        // The sequencer should be focused to receive events
        crownSequencer.focus()
        crownSequencer.delegate = self
    }

    @IBAction func sliderAction(_ value: Float) {
        selectedVal = value
    }
}

// MARK: WKCrownDelegate
extension SliderInterfaceController: WKCrownDelegate {
    func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
        // 1 divided by number of rotations required to change the value from min to max 
        let rotationToValRatio = 0.25 * Double(maxVal - minVal)

        let newVal = selectedVal + rotationalDelta * rotationToValRatio

        let trimmedNewVal = max(minVal, min(newVal, maxVal))

        slider.setValue(Float(trimmedNewVal))
        selectedVal = trimmedNewVal
    }
}