I want for my view model to have state to bind not in main view.
In the code below, I Binded view model's @GestureState variable to DragGesture in my main view. But the problem is, without any compile error, it does not updating my @GestureState variable at all. If I define the @GestureState variable in my main view, it work. Any idea?
class ViewModel {
@GestureState var dragOffset = CGSize.zero
}
struct ExampleView: View {
var viewModel = ViewModel()
var body: some View {
Image(systemName: "star.circle.fill")
.font(.system(size: 100))
.offset(x: viewModel.dragOffset.width, y: viewModel.dragOffset.height)
.animation(.easeInOut)
.foregroundColor(.green)
.gesture(
DragGesture()
.updating(viewModel.$dragOffset, body: { (value, state, transaction) in
print(state)
state = value.translation
print(state)
})
)
}
}