Hey guys,
I'm creating a fitness app on WatchOS and I need to realize a focus on a variable but it doesn't work.
I will show you my code to understand the problem :
@State var WeightChoose: Double = 0.0
@FocusState private var isWeightActive: Bool
var body: some View {
HStack {
Text("Poids:")
Spacer()
Text("\(String(format: "%.0f", WeightChoose)) KG")
.focused($isWeightActive)
.foregroundColor(isWeightActive ? .green : .white)
.digitalCrownRotation($WeightChoose, from: 0.0, through: 300.0, by: 1.0, sensitivity: .medium)
.animation(.easeIn(duration: 1), value: WeightChoose)
.padding()
Here my code don't show me any error but the focus doesn't work at all.
Before, I used the old way to focus with
@State private var isWeightActive = false
instead of @FocusState
and
.focusable(true) { focused in
isWeightActive = focused
}
Instead of .focused()
But now the .focusable() method is depreciated since watchOS 8 and we have to go on FocusState and .focused but it doesn't work for my code.
If you could help me with that it would be awesome.
Thank you
Cyrille
Hi Cyrille!
FocusState
works a little differently. Check out the documentation for more information and a really great example.
For watchOS, you'll probably want to look at the documentation for digitalCrownRotation
, and use the focusable
modifier underneath that.