Compare commits
4 Commits
ca374422db
...
6ee1ad24ec
Author | SHA1 | Date |
---|---|---|
Shadowfacts | 6ee1ad24ec | |
Shadowfacts | 78385359e1 | |
Shadowfacts | 808d3b3157 | |
Shadowfacts | 03d0556947 |
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public protocol SheetContainerViewControllerDelegate {
|
public protocol SheetContainerViewControllerDelegate: class {
|
||||||
func sheetContainer(_ sheetContainer: SheetContainerViewController, willSnapToDetent detent: Detent) -> Bool
|
func sheetContainer(_ sheetContainer: SheetContainerViewController, willSnapToDetent detent: Detent) -> Bool
|
||||||
func sheetContainer(_ sheetContainer: SheetContainerViewController, didSnapToDetent detent: Detent)
|
func sheetContainer(_ sheetContainer: SheetContainerViewController, didSnapToDetent detent: Detent)
|
||||||
func sheetContainerContentScrollView(_ sheetContainer: SheetContainerViewController) -> UIScrollView?
|
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
|
public let content: UIViewController
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ public class SheetContainerViewController: UIViewController {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override public func viewDidLoad() {
|
override open func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
||||||
dimmingView = UIView()
|
dimmingView = UIView()
|
||||||
|
@ -136,16 +136,16 @@ public class SheetContainerViewController: UIViewController {
|
||||||
let velocity = recognizer.velocity(in: scrollView)
|
let velocity = recognizer.velocity(in: scrollView)
|
||||||
|
|
||||||
let topContentOffset: CGFloat = delegate?.sheetContainer(self, topContentOffsetForScrollView: scrollView) ?? 0
|
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 shouldMoveSheetUp = topConstraint.constant > topDetent.offset && velocity.y < 0 // not fully expanded and dragging up
|
||||||
|
|
||||||
let shouldMoveSheet = shouldMoveSheetDown || shouldMoveSheetUp
|
let shouldMoveSheet = shouldMoveSheetDown || shouldMoveSheetUp
|
||||||
|
|
||||||
switch recognizer.state {
|
switch recognizer.state {
|
||||||
case .began:
|
case .began:
|
||||||
scrollView.bounces = false
|
|
||||||
scrollViewIsMovingSheet = shouldMoveSheet
|
scrollViewIsMovingSheet = shouldMoveSheet
|
||||||
if shouldMoveSheet {
|
if shouldMoveSheet {
|
||||||
|
scrollView.bounces = false
|
||||||
initialScrollViewContentOffset = scrollView.contentOffset
|
initialScrollViewContentOffset = scrollView.contentOffset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,6 +154,10 @@ public class SheetContainerViewController: UIViewController {
|
||||||
if scrollViewIsMovingSheet {
|
if scrollViewIsMovingSheet {
|
||||||
if initialScrollViewContentOffset == nil {
|
if initialScrollViewContentOffset == nil {
|
||||||
initialScrollViewContentOffset = scrollView.contentOffset
|
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)
|
scrollView.setContentOffset(initialScrollViewContentOffset!, animated: false)
|
||||||
|
|
||||||
|
@ -163,8 +167,8 @@ public class SheetContainerViewController: UIViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
case .ended:
|
case .ended:
|
||||||
scrollView.bounces = true
|
|
||||||
if scrollViewIsMovingSheet {
|
if scrollViewIsMovingSheet {
|
||||||
|
scrollView.bounces = true
|
||||||
scrollView.setContentOffset(initialScrollViewContentOffset!, animated: false)
|
scrollView.setContentOffset(initialScrollViewContentOffset!, animated: false)
|
||||||
springToNearestDetent(verticalVelocity: velocity.y)
|
springToNearestDetent(verticalVelocity: velocity.y)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue