Skip to content

Instantly share code, notes, and snippets.

@cliss
Last active May 12, 2023 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliss/201292734a90709ecbf857453ec86b52 to your computer and use it in GitHub Desktop.
Save cliss/201292734a90709ecbf857453ec86b52 to your computer and use it in GitHub Desktop.
List{} Gets Me Again
//: A UIKit based Playground for presenting user interface
import SwiftUI
import PlaygroundSupport
struct SlideDemo: View {
@State var activePage = 0
var body: some View {
// Change this VStack to a List and the animation breaks
VStack {
Picker("", selection: self.$activePage) {
Text("Left").tag(0)
Text("Right").tag(1)
}
.pickerStyle(.segmented)
.listRowSeparator(.hidden)
VStack {
if self.activePage == 0 {
Rectangle()
.fill(.green)
.frame(height: 100)
.transition(.move(edge: .trailing))
} else {
Rectangle()
.fill(.red)
.frame(height: 100)
.transition(.move(edge: .leading))
}
}
.id(143)
.listRowSeparator(.hidden)
}
.listStyle(.plain)
.animation(.easeInOut, value: self.activePage)
}
}
PlaygroundPage.current.setLiveView(SlideDemo())
@cliss
Copy link
Author

cliss commented May 12, 2023

Why is it changing the VStack above to a List breaks the animation?

Animation with VStack:

VStack

Animation with List:

List

@cliss
Copy link
Author

cliss commented May 12, 2023

Fixed in revision #2:

• Change GroupVStack
• Add an .id()

Siracusa

@andtie
Copy link

andtie commented May 12, 2023

Update: I See you found a different solution. Mine also requires changing the Group to something else (ZStack or VStack)

I think a List has its own implicit animations that overwrite your animations. It should work however by using an explicit animation. E.g.:

Picker("", selection: self.$activePage.animation(.easeInOut)) {
...
}

See also: https://www.objc.io/blog/2021/11/25/transactions-and-animations/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment