Skip to content

Commit

Permalink
πŸ”€ :: (#29) BottomSheet
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarldud1234 authored Feb 25, 2023
2 parents 5bd101a + 06fb2b9 commit f1e6766
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SwiftUI

struct SDBottomSheetPreview: View {
@State var isPresented: Bool = false
var body: some View {
Button {
isPresented = true
} label: {
Text("Test BottomSheet")
}
.sdBottomSheet(isPresented: $isPresented, buttons: [
(text: "μˆ˜μ •ν•˜κΈ°", action: { print("!!!!!") }),
(text: "μ‚­μ œν•˜κΈ°", action: { print("λ°˜κ°€μ›Œμš”") })
])
}
}

43 changes: 43 additions & 0 deletions SemicolonDesign/Sources/BottomSheet/SDBottomSheet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import SwiftUI

struct SDBottomSheet: View {
@Binding var isPresented: Bool
var buttons: [(text: String, action: () -> Void)]

init(
isPresented: Binding<Bool>,
buttons: [(text: String, action: () -> Void)]
) {
self.buttons = buttons
self._isPresented = isPresented
}

var body: some View {
VStack(alignment: .leading, spacing: 0) {
ForEach(buttons, id: \.text) { button in
Button{
self.isPresented = false
button.action()
} label: {
HStack {
Text(button.text)
Spacer()
}
}
.padding(.bottom, 26)
}
}
.foregroundColor(.black)
.padding(.leading, 29)
.padding(.top, 36)
.padding(.bottom, 4)
.background(Color.white)
.cornerRadius(16, [.topLeft, .topRight])
}
}

struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SDBottomSheetPreview()
}
}
28 changes: 28 additions & 0 deletions SemicolonDesign/Sources/BottomSheet/SDBottomSheetModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import SwiftUI

struct SDBottomSheetModifier: ViewModifier {

var isPresented: Binding<Bool>
var buttons: [(text: String, action: () -> Void)]

func body(content: Content) -> some View {
ZStack {
content
if isPresented.wrappedValue {
Color.black.opacity(0.4)
.ignoresSafeArea()
VStack {
Spacer()
Color.white
.edgesIgnoringSafeArea(.bottom)
.frame(height: CGFloat(40))
}
VStack(spacing: 0) {
Spacer()
SDBottomSheet(isPresented: isPresented, buttons: buttons)
}
.animation(.easeOut, value: isPresented.wrappedValue)
}
}
}
}
7 changes: 7 additions & 0 deletions SemicolonDesign/Sources/BottomSheet/View+sdBottomSheet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import SwiftUI

public extension View {
func sdBottomSheet(isPresented: Binding<Bool>, buttons: [(text: String, action: () -> Void)]) -> some View {
self.modifier(SDBottomSheetModifier(isPresented: isPresented, buttons: buttons))
}
}
23 changes: 23 additions & 0 deletions SemicolonDesign/Sources/Extension/View+cornerRadius.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import SwiftUI

extension View {
func cornerRadius(_ radius: CGFloat, _ corners: UIRectCorner) -> some View {
clipShape(RoundedCorner(radius: radius, corners: corners))
}
}

struct RoundedCorner: Shape {

var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners

func path(in rect: CGRect) -> Path {
let path = UIBezierPath(
roundedRect: rect,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius)
)
return Path(path.cgPath)
}

}

0 comments on commit f1e6766

Please sign in to comment.