【问题标题】:How to read the physical Action button of the Apple Watch Ultra?如何读取 Apple Watch Ultra 的物理 Action 按钮?
【发布时间】:2022-10-23 02:50:15
【问题描述】:

有没有办法在 SwiftUI 中读取 Apple Watch Ultra 的操作按钮?

您可以像下面的示例中那样阅读 Digital Crown,但我没有发现 Action 按钮类似的东西?

struct DigitalCrown: View {
    @State private var crownValue = 0.0

    var body: some View {
        Text("Received Value:\(crownValue, specifier: "%.1f")")
            .focusable()
            .digitalCrownRotation($crownValue)
    }
}

【问题讨论】:

标签: swift swiftui apple-watch watchos


【解决方案1】:

我还没有找到如何指定一个动作,但有一个 UI 测试。

XCUIDevice.current.press(.action)

https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

从措辞我猜这可能是一个“捷径”

【讨论】:

    【解决方案2】:

    Apple Watch Ultra 的操作按钮由新的 App Intents API 提供支持。您将能够构建自己的应用程序以与之集成,例如使用按钮记录目标的曲棍球应用程序。对于用户而言,该按钮可以启动您想要的任何快捷方式。

    要使用它,请创建一个符合 StarWorkoutIntent 协议的 Intent:https://developer.apple.com/documentation/appintents/startworkoutintent

    这些 API 适用于 Workout 应用程序,因此要将这个新按钮置于控制您的应用程序的模式下,您至少需要从 StartWorkoutIntent 开始。

    完成此操作后,您可以使用 actionButtonIntent 创建 AppIntent 结果,以将按钮将执行的下一件事设置为任何其他意图。或者,如果您要根据应用 UI 中发生的事情来更改按钮的行为,则可以使用捐赠来执行此操作。

    https://twitter.com/mgorbach/status/1567570300820144131

    【讨论】:

    • 您能否详细说明“使用 actionButtonIntent 创建 AppIntent 结果”的含义?
    【解决方案3】:

    我下载并测试了您可以在此处找到的示例: https://github.com/KhaosT/WatchActionButtonExample

    它在创建意图方面工作正常,因此您的应用可以链接到 WatchOS 设置中的物理按钮,但它并没有真正显示如何将意图链接到用户界面中的某些操作。

    有人知道怎么做吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多