gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:32:13 33 4
gpt4 key购买 nike

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

如何才能有一个函数对冠值变化使用react以执行网络请求?

谢谢

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

}

}
}

最佳答案

实际上,如果您导入Combine,您可以执行以下操作:

[...]
.focusable(true)
.digitalCrownRotation($crownValue, from: 0.1, through: 5.0, sensitivity: .low, isContinuous: false, isHapticFeedbackEnabled: true)
.onReceive(Just(crownValue)) { output in
print(output) // Here is the answer.
}

关于swift - 如何使用 SwiftUI 从 Apple Watch Digital Crown 触发 @State 变量更改的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905657/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com