I have used the .gesture for dragging the piece of text.
What I want to happen is the remaining letters to move to the right such that 'g' takes the first position and the remaining letters move towards the right. But I am unable to figure out how I should do that.
struct TryingDrag: View {
let letters = Array("Begin Saving")
@State private var dragAmount = CGSize.zero
var body: some View {
HStack (spacing: 0) {
ForEach(0..<letters.count) { index in
let letter = String(letters[index])
LettersDrag(letter: letter)
}
}
}
}
struct LettersDrag: View {
let letter: String
@State private var dragAmount = CGSize.zero
var body: some View {
Text(letter).foregroundColor(.white)
.padding(5)
.font(.title)
.background(Color.red)
.offset(dragAmount)
.animation(.spring())
.gesture(
DragGesture()
.onChanged {
dragAmount = $0.translation
}
.onEnded { _ in
dragAmount = .zero
}
)
}
}
I want it to have similar behaviour to the following image (but without using list/forms):