Hello everyone-
So I have been struggling with a scripting concept, making objects draggable in SwiftUI in Xcode. I would like to figure out the script to make an image be able to be dragged around the screen by an app user...could I get some help?
Hello everyone-
So I have been struggling with a scripting concept, making objects draggable in SwiftUI in Xcode. I would like to figure out the script to make an image be able to be dragged around the screen by an app user...could I get some help?
Here are the principles for such a drag. If that does not work, please explain the problem. Otherwise, don't forget to close the thread.
Create a few state var:
@State private var location: CGPoint = CGPoint(x: 200, y: 200) // just to have an inital value
@GestureState private var fingerLocation: CGPoint? = nil
@GestureState private var startLocation: CGPoint? = nil
Create the dragGestures:
var simpleDrag: some Gesture {
DragGesture()
.onChanged { value in
var newLocation = startLocation ?? location
newLocation.x += value.translation.width
newLocation.y += value.translation.height
self.location = newLocation
}
.updating($startLocation) { (value, startLocation, transaction) in
startLocation = startLocation ?? location
}
}
var fingerDrag: some Gesture {
DragGesture()
.updating($fingerLocation) { (value, fingerLocation, transaction) in
fingerLocation = value.location
}
}
Then apply modifiers to image:
.position(location)
.gesture(simpleDrag.simultaneously(with: fingerDrag))