Compare commits

...

4 Commits

1 changed files with 11 additions and 7 deletions

View File

@ -8,7 +8,7 @@
import UIKit
public protocol SheetContainerViewControllerDelegate {
public protocol SheetContainerViewControllerDelegate: class {
func sheetContainer(_ sheetContainer: SheetContainerViewController, willSnapToDetent detent: Detent) -> Bool
func sheetContainer(_ sheetContainer: SheetContainerViewController, didSnapToDetent detent: Detent)
func sheetContainerContentScrollView(_ sheetContainer: SheetContainerViewController) -> UIScrollView?
@ -29,9 +29,9 @@ public extension SheetContainerViewControllerDelegate {
}
}
public class SheetContainerViewController: UIViewController {
open class SheetContainerViewController: UIViewController {
public var delegate: SheetContainerViewControllerDelegate?
public weak var delegate: SheetContainerViewControllerDelegate?
public let content: UIViewController
@ -72,7 +72,7 @@ public class SheetContainerViewController: UIViewController {
fatalError("init(coder:) has not been implemented")
}
override public func viewDidLoad() {
override open func viewDidLoad() {
super.viewDidLoad()
dimmingView = UIView()
@ -136,16 +136,16 @@ public class SheetContainerViewController: UIViewController {
let velocity = recognizer.velocity(in: scrollView)
let topContentOffset: CGFloat = delegate?.sheetContainer(self, topContentOffsetForScrollView: scrollView) ?? 0
let shouldMoveSheetDown = scrollView.contentOffset.y <= topContentOffset && velocity.y > 0 // scrolled to top and dragging down
let shouldMoveSheetDown = scrollView.contentOffset.y <= -topContentOffset && velocity.y > 0 // scrolled to top and dragging down
let shouldMoveSheetUp = topConstraint.constant > topDetent.offset && velocity.y < 0 // not fully expanded and dragging up
let shouldMoveSheet = shouldMoveSheetDown || shouldMoveSheetUp
switch recognizer.state {
case .began:
scrollView.bounces = false
scrollViewIsMovingSheet = shouldMoveSheet
if shouldMoveSheet {
scrollView.bounces = false
initialScrollViewContentOffset = scrollView.contentOffset
}
@ -154,6 +154,10 @@ public class SheetContainerViewController: UIViewController {
if scrollViewIsMovingSheet {
if initialScrollViewContentOffset == nil {
initialScrollViewContentOffset = scrollView.contentOffset
// if the sheet begins moving part way through the gesture, we need to ignore any translation
// that's already taken place, otherwise the sheet will jump when it starts moving
recognizer.setTranslation(.zero, in: scrollView)
}
scrollView.setContentOffset(initialScrollViewContentOffset!, animated: false)
@ -163,8 +167,8 @@ public class SheetContainerViewController: UIViewController {
}
case .ended:
scrollView.bounces = true
if scrollViewIsMovingSheet {
scrollView.bounces = true
scrollView.setContentOffset(initialScrollViewContentOffset!, animated: false)
springToNearestDetent(verticalVelocity: velocity.y)
}