I have a Rectangular which I can easily read local location of tap while animation is not alive, the issue is there that when animation is alive the given value from SwiftUI is not the local location of tap but it shows some value related to the local location.
For example the size of Rectangular is 200X200 so normally without animation it shows a value from 0 to 200 depending on where we taped! but with animation it shows negative or bigger than 200 value which is impossible as local location, I know that SwiftUI try to show the current value of local location, but this data is not useful for me, I need to know how can I change this value to the from that shows the local location between 0 in 200.
struct ContentView: View {
@State private var offset: CGFloat = -300.0
@State private var animationID: Bool = false
var body: some View {
return Color.yellow
.overlay(
Color.blue
.frame(width: 200.0, height: 200.0, alignment: .center)
.cornerRadius(10.0)
.gesture(localGesture)
.offset(y: offset)
.animation(Animation.linear(duration: 10.0), value: offset)
.id(animationID)
)
.ignoresSafeArea()
.overlay(
Button((offset == -300.0) ? "down" : "up") {
animationID.toggle()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(200)) {
if (offset == -300.0) { offset = 0.0 }
else { offset = -300.0 }
}
}
.font(Font.body.bold())
.padding()
, alignment: .bottom)
}
var localGesture: some Gesture {
DragGesture(minimumDistance: 0.0, coordinateSpace: .local)
.onEnded { localValue in
print("location.y:", localValue.location.y)
}
}
}