如何使用SwiftUI从Apple Watch Digital Crown对@State变量更改触发函数?

扎因·艾哈迈德(Zain Ahmed)

下面的代码在SwiftUI中创建了一个环,当旋转数字表冠时该环就完成了。我想在“ crownValue”状态变量的值达到5.0(即圆圈完全可见)时触发一个函数。

我该如何具有对表冠值变化做出反应的功能以执行Web请求?

谢谢

struct commandDetailView: View {

    @State var crownValue = 0.1


    var body: some View {
        VStack {
            Circle()
                .trim(from: 0.0, to: CGFloat(crownValue / 5.0))
                .stroke(Color.blue, style: StrokeStyle(lineWidth: 12, lineCap: CGLineCap.round, lineJoin: .round))
                .frame(width: 120, height: 120)

            Text("Spin to Start Car")


        }
    .focusable(true)
        .digitalCrownRotation($crownValue, from: 0.1, through: 5.0, sensitivity: .low, isContinuous: false, isHapticFeedbackEnabled: true)
        .onAppear() {

        }

    }
}
Arsenius

不幸的是,现在最容易做的事情是使用包装您的的自定义绑定crownValue(我很遗憾地说,因为我与苹果公司就缺少didSetfor@State以及如何解决它问题持公开门票,但尚未解决。)


var crownValueBinding: Binding<Double> {
  Binding<Double>(
    get: { self.crownValue },
    set: { newValue in
      self.crownValue = newValue
      self.myFunc() // Whatever you like
    }
  )
}

并使用它:

 SomeView()
   .digitalCrownRotation(self.crownValueBinding, ...) // NOTE: No $ sign!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章