Created
July 19, 2020 22:27
-
-
Save mattyoung/fd501b964c029c874343c6c2844240bd to your computer and use it in GitHub Desktop.
This file contains 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 | |
struct SimpleAnimation: View { | |
// In Xcode preview? animation doesn't work | |
// animate only if these are set to true | |
// why when set to false, no animation? | |
@State var flag = false | |
@State var anotherFlag = true | |
var body: some View { | |
ZStack { | |
// add this ZStack, `if flag` animation only animates true branch, not false branch | |
// ZStack { | |
// Color.green | |
//// if flag { | |
//// Color.blue | |
//// } else { | |
//// Color.green | |
//// } | |
// } | |
// add this, anything that's initially not in the view (on false branch) never become visible | |
// Color.green | |
// if flag { | |
// Color.blue | |
// } else { | |
// Color.green | |
// } | |
// Add a ZStack above, this only animate the true branch, no false branch | |
if flag { | |
Text("👍True!") | |
.font(.system(size: 90)) | |
.foregroundColor(.orange) | |
} else { | |
Text("False?🥺") | |
.font(.system(size: 90)) | |
.foregroundColor(.purple) | |
.padding(.top, 200) | |
} | |
// if flag { | |
// Text("🤘True!") | |
// .font(.system(size: 90)) | |
// .foregroundColor(.black) | |
// } | |
// if anotherFlag { | |
// Text("False...🤠") | |
// .font(.system(size: 90)) | |
// .foregroundColor(.orange) | |
// .padding(.top, 200) | |
// } | |
} | |
.onAppear { | |
withAnimation(Animation.linear(duration: 2).repeatForever()) { | |
// calling toggle() to flip from whatever true/false initial state | |
flag.toggle() | |
anotherFlag.toggle() | |
} | |
} | |
} | |
} | |
struct SimpleAnimation_Previews: PreviewProvider { | |
static var previews: some View { | |
SimpleAnimation() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment