Skip to content

Instantly share code, notes, and snippets.

@rxtr007
Forked from Dimillian/navigation+modal.swift
Created October 16, 2019 04:34
Show Gist options
  • Select an option

  • Save rxtr007/04e9610dc203492f1e4a7a6a14962141 to your computer and use it in GitHub Desktop.

Select an option

Save rxtr007/04e9610dc203492f1e4a7a6a14962141 to your computer and use it in GitHub Desktop.

Revisions

  1. @Dimillian Dimillian created this gist Oct 14, 2019.
    116 changes: 116 additions & 0 deletions navigation+modal.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    //
    // ContentView.swift
    // navigation
    //
    // Created by Thomas Ricouard on 13/10/2019.
    // Copyright © 2019 Thomas Ricouard. All rights reserved.
    //

    import SwiftUI

    struct RootView: View {
    @State private var isHomeShown = true
    @State private var isSettingShown = false
    @State private var selectedContent = "content1"

    var body: some View {
    ZStack {
    ContentView(content: $selectedContent,
    isHomeShown: $isHomeShown,
    isSettingShown: $isSettingShown)
    .blur(radius: isHomeShown ? 10 : 0)
    .scaleEffect(isHomeShown ? 0.8 : 1)
    .animation(.spring())
    if isHomeShown {
    HomeView(selectedContent: $selectedContent,
    isHomeShown: $isHomeShown)
    .transition(.scale)
    .animation(.spring())
    }
    if isSettingShown {
    SettingsView(isShown: $isSettingShown)
    .transition(.slide)
    .animation(.spring())
    }
    }
    }
    }

    struct HomeView: View {
    let contents = ["content1", "content2", "content3", "content4"]
    @Binding var selectedContent: String
    @Binding var isHomeShown: Bool

    var body: some View {
    ScrollView(.horizontal, showsIndicators: false) {
    HStack(spacing: 16) {
    ForEach(contents, id: \.self) { content in
    Rectangle()
    .foregroundColor(.blue)
    .frame(width: 100, height: 150)
    .onTapGesture {
    self.selectedContent = content
    self.isHomeShown = false
    }
    }
    }.padding()
    }
    }
    }

    struct ContentView: View {
    @Binding var content: String
    @Binding var isHomeShown: Bool
    @Binding var isSettingShown: Bool

    var body: some View {
    GeometryReader { _ in
    VStack {
    Text(self.content)
    Button(action: {
    self.isHomeShown = true
    }, label: {
    Text("Back").foregroundColor(.blue)
    })
    Spacer()
    Button(action: {
    self.isSettingShown = true
    }, label: {
    Text("Settings").foregroundColor(.blue)
    })
    .padding()
    }
    }
    .background(Color.yellow)
    .frame(width: 300, height: 300)
    }
    }

    struct SettingsView: View {
    @Binding var isShown: Bool

    var body: some View {
    ZStack {
    RoundedRectangle(cornerRadius: 20)
    .fill(Color.white)
    .shadow(radius: 10)
    VStack {
    Text("Setting 1")
    Text("Setting 2")
    Button(action: {
    self.isShown = false
    }, label: {
    Text("Close").foregroundColor(.blue)
    })
    }
    .padding()
    }
    .frame(width: 250, height: 250)
    }
    }

    struct RootView_Previews: PreviewProvider {
    static var previews: some View {
    RootView()
    }
    }