Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Created August 21, 2019 11:39
Show Gist options
  • Select an option

  • Save PaulWoodIII/a68928afdaa997c4d45ef4fe156c4d4b to your computer and use it in GitHub Desktop.

Select an option

Save PaulWoodIII/a68928afdaa997c4d45ef4fe156c4d4b to your computer and use it in GitHub Desktop.
SwiftUI Swap the position of an element in a Stack View with animation
import SwiftUI
extension String: Identifiable {
public var id: String { return self }
}
struct ContentView: View {
@State var list: [String] = ["Hello", "World"]
var body: some View {
VStack{
ForEach(list) { str in
Text(str)
}
Button(action: {
self.list.swapAt(0, 1)
}) {
Text("Animate")
}
}.animation(.default)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment