Created
May 20, 2020 17:58
-
-
Save duemunk/ab86c0f54c42990553584ca7b1c00564 to your computer and use it in GitHub Desktop.
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 | |
// 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