Skip to content

Commit

Permalink
Calculate container size and close sheet if offset > height / 2
Browse files Browse the repository at this point in the history
  • Loading branch information
aram-azbekian committed Feb 26, 2023
1 parent f3d1554 commit 2d95831
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChatlaxSheet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
24206D97299167F6002ACD33 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 24206D96299167F6002ACD33 /* Assets.xcassets */; };
24206D9A299167F6002ACD33 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 24206D99299167F6002ACD33 /* Preview Assets.xcassets */; };
24206DA129916AB8002ACD33 /* BottomSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24206DA029916AB8002ACD33 /* BottomSheetView.swift */; };
2429B1B329ABD31D009A732B /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2429B1B229ABD31D009A732B /* Utils.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -21,6 +22,7 @@
24206D96299167F6002ACD33 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
24206D99299167F6002ACD33 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
24206DA029916AB8002ACD33 /* BottomSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BottomSheetView.swift; sourceTree = "<group>"; };
2429B1B229ABD31D009A732B /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -58,6 +60,7 @@
24206D96299167F6002ACD33 /* Assets.xcassets */,
24206D98299167F6002ACD33 /* Preview Content */,
24206DA029916AB8002ACD33 /* BottomSheetView.swift */,
2429B1B229ABD31D009A732B /* Utils.swift */,
);
path = ChatlaxSheet;
sourceTree = "<group>";
Expand Down Expand Up @@ -143,6 +146,7 @@
24206D95299167F6002ACD33 /* ContentView.swift in Sources */,
24206D93299167F6002ACD33 /* ChatlaxSheetApp.swift in Sources */,
24206DA129916AB8002ACD33 /* BottomSheetView.swift in Sources */,
2429B1B329ABD31D009A732B /* Utils.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
7 changes: 6 additions & 1 deletion ChatlaxSheet/BottomSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct BottomSheetView: View {

@Binding var isChatlaxSheetPresented: Bool
@State private var offset: CGFloat = .zero
@State private var size: CGSize = .zero

var body: some View {
ZStack {
Expand All @@ -34,6 +35,7 @@ struct BottomSheetView: View {
.foregroundColor(Color.black)
.padding(.top, 10)
}
.saveSize(in: $size)
.transition(.move(edge: .bottom))
.gesture(
DragGesture()
Expand All @@ -43,7 +45,10 @@ struct BottomSheetView: View {
}
})
.onEnded({ _ in
if offset <= -20 {
if offset <= size.height / 2 {
offset = 0
} else {
isChatlaxSheetPresented.toggle()
offset = 0
}
})
Expand Down
32 changes: 32 additions & 0 deletions ChatlaxSheet/Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Utils.swift
// ChatlaxSheet
//
// Created by Арам on 26.02.2023.
//

import SwiftUI

struct SizeCalculator: ViewModifier {

@Binding var size: CGSize

func body(content: Content) -> some View {
content
.background(
GeometryReader { proxy in
Color.clear
.onAppear {
size = proxy.size
}
}
)
}

}

extension View {
func saveSize(in size: Binding<CGSize>) -> some View {
modifier(SizeCalculator(size: size))
}
}

0 comments on commit 2d95831

Please sign in to comment.