I want update a State value with a function, in this function I want use a DispatchQueue but I am getting this error:
Escaping closure captures 'inout' parameter 'currentValue'
How can I solve this issue?
struct ContentView: View {
@State private var offset: CGFloat = CGFloat()
var body: some View {
Color.yellow
.overlay(
Circle()
.fill(Color.red)
.frame(width: 100.0, height: 100.0, alignment: .center)
.offset(y: offset)
)
.overlay(
Button("update") {
updater(currentValue: &offset, goalValue: 300.0)
}
.font(Font.body.bold())
.padding()
, alignment: .bottom)
}
}
func updater(currentValue: inout CGFloat, goalValue: CGFloat) {
if (currentValue + 1.0) <= goalValue {
currentValue += 1.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(1000)) {
updater(currentValue: ¤tValue, goalValue: goalValue)
}
}
}