Skip to content

Instantly share code, notes, and snippets.

@CrystalKnightCodes
Created September 28, 2021 16:02
Show Gist options
  • Save CrystalKnightCodes/2f63e650c299457e701f5f24143a8cd1 to your computer and use it in GitHub Desktop.
Save CrystalKnightCodes/2f63e650c299457e701f5f24143a8cd1 to your computer and use it in GitHub Desktop.
Animations and Transitions
import SwiftUI
struct ContentView: View {
// MARK: - Properties
@State private var showDetail = false
// MARK: - View
var body: some View {
NavigationView {
VStack(spacing: 10) {
Image(systemName: "photo")
.resizable()
.frame(maxWidth: 200, maxHeight: 200)
Spacer()
Text("Static Words")
Spacer()
Button("Next") { showDetail = true }
.foregroundColor(.blue)
NavigationLink(isActive: $showDetail) {
DetailView(isPresented: $showDetail)
} label: { EmptyView() }
}
.foregroundColor(.black)
.font(.largeTitle)
}
}
}
// MARK: - Preview
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI
struct DetailView: View {
// MARK: - Properties
@State private var showDetail = false
@Binding var isPresented: Bool
// MARK: - View
var body: some View {
VStack(spacing: 10) {
Image(systemName: "photo")
.resizable()
.frame(maxWidth: 200, maxHeight: 200)
Spacer()
Text("Animated Text")
.transition(.opacity)
.hidden(!isPresented)
Spacer()
Button("Next") { showDetail = true }
.foregroundColor(.blue)
NavigationLink(isActive: $showDetail) {
Text("Next Page")
} label: { EmptyView() }
}
.foregroundColor(.black)
.font(.largeTitle)
}
}
// MARK: - Preview
struct DetailView_Previews: PreviewProvider {
static var previews: some View {
DetailView(isPresented: .constant(true))
}
}
import SwiftUI
extension View
{
/// Use a Bool to determine whether or not a view should be hidden.
/// - Parameter shouldHide: Bool
/// - Returns: some View
@ViewBuilder func hidden(_ shouldHide: Bool) -> some View {
switch shouldHide
{
case true:
self.hidden()
case false:
withAnimation {
self.animation(.easeIn(duration: 1))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment