Skip to content

Commit

Permalink
Hide sheet when near the bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Jun 6, 2022
1 parent 8352aa4 commit 1a115f9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "dynamic-wang-KZuZAaEojq4-unsplash.jpg",
"filename" : "harold-wainwright-aEK8X33l7V4-unsplash (1).jpg",
"idiom" : "universal",
"scale" : "1x"
},
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions Example/SplitSheetExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class ViewController: UIViewController {
super.viewDidLoad()

/// If true, `mainViewController` will shift up as the sheet is shown.
splitSheetController.displaceContent = false
splitSheetController.displaceContent = true

/// Show a grabber handle.
splitSheetController.showHandle = true

/// The minimum sheet height.
splitSheetController.minimumSheetHeight = CGFloat(300)
splitSheetController.minimumSheetHeight = CGFloat(400)

/// When the sheet is shown and dragged within this limit, the sheet will bounce back.
splitSheetController.snappingDistance = CGFloat(150)
Expand All @@ -50,7 +50,7 @@ class ViewController: UIViewController {
splitSheetController.swipeUpToShowAllowed = true

/// Override the status bar color.
splitSheetController.statusBarStyle = UIStatusBarStyle.lightContent
splitSheetController.statusBarStyle = UIStatusBarStyle.default

/// Add the sheet.
embed(splitSheetController, inside: view)
Expand All @@ -62,6 +62,7 @@ class MainViewController: UIViewController {
/// image credits:
/// https://unsplash.com/photos/aEK8X33l7V4
/// https://unsplash.com/photos/bokTMevSgxY
/// https://unsplash.com/photos/KZuZAaEojq4
let image = UIImage(named: "Image")
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
Expand Down
10 changes: 9 additions & 1 deletion Sources/SplitSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ extension SplitSheetController: UIScrollViewDelegate {
let detents = [hiddenDetent, shownDetent, expandedDetent]
let predictedContentOffset = targetContentOffset.pointee.y /// Use the system-predicted target content to take care of flicking with the finger.

print("distanceToShown: \(distanceToShown)")
switch velocity.y {
/// When the velocity is negative (scrolled down.)
case ..<0:
Expand All @@ -298,11 +299,18 @@ extension SplitSheetController: UIScrollViewDelegate {

/// Handle velocity == 0 case.
default:

/// Ignore if sheet was released near the `expanded` detent.
guard distanceToShown > -snappingDistance else { return }

if distanceToShown < snappingDistance {
targetContentOffset.pointee.y = minimumSheetHeight
} else {}
} else {
targetContentOffset.pointee.y = 0 /// Hide if near the bottom.
}
}

/// When the offset is 0, the sheet is hidden. Otherwise, it's shown.
updateShowing(targetContentOffset.pointee.y != 0)
}
}
Expand Down

0 comments on commit 1a115f9

Please sign in to comment.