Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created October 23, 2020 15:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/20536c74735ba03ebeb1a06a1050ede7 to your computer and use it in GitHub Desktop.
Save BetterProgramming/20536c74735ba03ebeb1a06a1050ede7 to your computer and use it in GitHub Desktop.
struct SideBarStack<SidebarContent: View, Content: View>: View {
let sidebarContent: SidebarContent
let mainContent: Content
let sidebarWidth: CGFloat
@Binding var showSidebar: Bool
init(sidebarWidth: CGFloat, showSidebar: Binding<Bool>, @ViewBuilder sidebar: ()->SidebarContent, @ViewBuilder content: ()->Content) {
self.sidebarWidth = sidebarWidth
self._showSidebar = showSidebar
sidebarContent = sidebar()
mainContent = content()
}
var body: some View {
ZStack(alignment: .leading) {
sidebarContent
.frame(width: sidebarWidth, alignment: .center)
.offset(x: showSidebar ? 0 : -1 * sidebarWidth, y: 0)
.animation(Animation.easeInOut.speed(2))
mainContent
.overlay(
Group {
if showSidebar {
Color.white
.opacity(showSidebar ? 0.01 : 0)
.onTapGesture {
self.showSidebar = false
}
} else {
Color.clear
.opacity(showSidebar ? 0 : 0)
.onTapGesture {
self.showSidebar = false
}
}
}
)
.offset(x: showSidebar ? sidebarWidth : 0, y: 0)
.animation(Animation.easeInOut.speed(2))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment