Skip to content

Instantly share code, notes, and snippets.

@duemunk
Created May 20, 2020 17:58
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 duemunk/ab86c0f54c42990553584ca7b1c00564 to your computer and use it in GitHub Desktop.
Save duemunk/ab86c0f54c42990553584ca7b1c00564 to your computer and use it in GitHub Desktop.
import SwiftUI
// Animates nicely since the type of both returns are the same, i.e. AnyView<Text>
func gimme(toggle: Bool) -> AnyView {
if toggle {
return AnyView(
Text("Hello World")
)
} else {
return AnyView(
Text("Hello Worlds")
)
}
}
// Doesn't animate, since the two Text views are in separate conditionals
func gimme2(toggle: Bool) -> AnyView {
AnyView (
Group {
if toggle {
Text("Hello World")
} else {
Text("Hello Worlds")
}
}
)
}
struct Animator: View {
@State var toggle: Bool = true
var body: some View {
VStack {
Toggle(isOn: $toggle) {
Text("Toggle")
}
Divider()
VStack {
Text("Animates")
gimme(toggle: toggle)
.foregroundColor(.white)
.background(Color.black)
}
Divider()
VStack {
Text("Doesn't animate (fade)")
gimme2(toggle: toggle)
.foregroundColor(.white)
.background(Color.black)
}
}
.frame(width: 600, height: 500)
.animation(.easeIn(duration: 1))
}
}
// For Playground
import PlaygroundSupport
PlaygroundPage.current.liveView = NSHostingView(rootView: Animator())
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment