Reputation: 8995
Swift 5, iOS 13.x
I got this super simple app that lets me drag a view around by using the offset with SwiftUI, and when it stops I reset the offset and the position. Works well the first time, but each time I go back to drag it again it jumps when I release the button. How can I fix this ... I found something that mentioned insets? Is it related to insets?
Now I know this answers the question with some different code kontiki but I want to understand what is wrong with my solution.
struct ContentView: View {
@State var dragOffset = CGSize.zero
@State var position:CGPoint = CGPoint(x:0,y:0)
var body: some View {
VStack {
Circle()
.frame(width: 12, height: 128, alignment: .center)
}.offset(x: self.dragOffset.width, y: self.dragOffset.height)
.gesture(DragGesture(coordinateSpace: .global)
.onChanged({ ( value ) in
self.dragOffset = CGSize(width: value.translation.width, height: value.translation.height)
})
.onEnded { ( value ) in
self.dragOffset = .zero
self.position = value.location
}
).position(position)
}
}
Upvotes: 3
Views: 2478
Reputation: 46
My gut says that you are likely dealing with an issue that has to do with the SafeArea EdgeInsets. It's hard to tell. If you're interested, I did two videos on the drag gesture and the tap gesture to create views when you tap and to drag them if you tap on a previously generated view:
https://www.youtube.com/watch?v=x3mzLhPfLaE&list=PLEHYNiA7SPcP9zzQYHAN1T27w4cwG_f5i
https://www.youtube.com/watch?v=V0F5ZuJgzoY&list=PLEHYNiA7SPcP9zzQYHAN1T27w4cwG_f5i&index=2
Upvotes: 1