SwiftUI Digital Crown 步进数字不起作用
SwiftUI Digital Crown stepping through numbers not working
我把脸撞在墙上试图弄清楚为什么我无法在 Swift 中的 Text
UI 组件上使用 .digitalCrownRotation
功能UI 对于 WatchOS。
这是我的代码:
import SwiftUI
struct ButtonView: View {
@State private var isFocused = false
@State private var value: Int = 0
var body: some View {
Button(action: {
print("clicked")
}) {
Text("\(value)")
.contentShape(Rectangle())
.focusable { self.isFocused = [=10=] }
.digitalCrownRotation(self.$value, from: 0, through: 9, by: 1, sensitivity: .medium, isContinuous: true, isHapticFeedbackEnabled: true)
}
.background(self.isFocused ? Color.green : Color.white)
}
}
一切正常,直到我尝试添加 .digitalCrownRotation
功能的指针。
每当我尝试构建时,我都会遇到以下 2 条构建失败消息:
Argument type 'Int.Stride' (aka 'Int') does not conform to expected type 'BinaryFloatingPoint'
Argument type 'Int' does not conform to expected type 'BinaryFloatingPoint'
我基本上是在尝试使用数字表冠在按钮聚焦时逐步显示从 0 到 9 的数字(整数)。但它不起作用,我不确定如何解决它。
这里是声明
public func digitalCrownRotation<V>(_ binding: Binding<V>, from minValue: V, through
maxValue: V, by stride: V.Stride? = nil, sensitivity: DigitalCrownRotationalSensitivity = .high,
isContinuous: Bool = false, isHapticFeedbackEnabled: Bool = true) -> some View
where V : BinaryFloatingPoint, V.Stride : BinaryFloatingPoint ```
所以如所见和错误报告 V
类型(从您提供的绑定中检测到)应该是浮点数,但您的 value
是 Int
,这不是浮点数, 所以修复很简单
@State private var value: Double = 0
我把脸撞在墙上试图弄清楚为什么我无法在 Swift 中的 Text
UI 组件上使用 .digitalCrownRotation
功能UI 对于 WatchOS。
这是我的代码:
import SwiftUI
struct ButtonView: View {
@State private var isFocused = false
@State private var value: Int = 0
var body: some View {
Button(action: {
print("clicked")
}) {
Text("\(value)")
.contentShape(Rectangle())
.focusable { self.isFocused = [=10=] }
.digitalCrownRotation(self.$value, from: 0, through: 9, by: 1, sensitivity: .medium, isContinuous: true, isHapticFeedbackEnabled: true)
}
.background(self.isFocused ? Color.green : Color.white)
}
}
一切正常,直到我尝试添加 .digitalCrownRotation
功能的指针。
每当我尝试构建时,我都会遇到以下 2 条构建失败消息:
Argument type 'Int.Stride' (aka 'Int') does not conform to expected type 'BinaryFloatingPoint' Argument type 'Int' does not conform to expected type 'BinaryFloatingPoint'
我基本上是在尝试使用数字表冠在按钮聚焦时逐步显示从 0 到 9 的数字(整数)。但它不起作用,我不确定如何解决它。
这里是声明
public func digitalCrownRotation<V>(_ binding: Binding<V>, from minValue: V, through maxValue: V, by stride: V.Stride? = nil, sensitivity: DigitalCrownRotationalSensitivity = .high, isContinuous: Bool = false, isHapticFeedbackEnabled: Bool = true) -> some View where V : BinaryFloatingPoint, V.Stride : BinaryFloatingPoint ```
所以如所见和错误报告 V
类型(从您提供的绑定中检测到)应该是浮点数,但您的 value
是 Int
,这不是浮点数, 所以修复很简单
@State private var value: Double = 0