Created
August 21, 2019 11:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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