Skip to content

Commit

Permalink
Merge pull request #23 from finn-no/feature/dragable-height
Browse files Browse the repository at this point in the history
Draggable height as configuration option
vadymmarkov authored Jan 15, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents f61ec84 + 64f3595 commit aab1f2c
Showing 4 changed files with 50 additions and 6 deletions.
13 changes: 11 additions & 2 deletions Sources/BottomSheetNavigationController.swift
Original file line number Diff line number Diff line change
@@ -10,11 +10,20 @@ open class BottomSheetNavigationController: UINavigationController {

// MARK: - Init

public init(rootViewController: UIViewController, useSafeAreaInsets: Bool = false) {
public init(
rootViewController: UIViewController,
handleBackground: BottomSheetView.HandleBackground = .color(.clear),
draggableHeight: CGFloat? = nil,
useSafeAreaInsets: Bool = false,
stretchOnResize: Bool = false
) {
super.init(rootViewController: rootViewController)
bottomSheetTransitioningDelegate = BottomSheetTransitioningDelegate(
contentHeights: [systemLayoutSizeFittingHeight(for: rootViewController)],
useSafeAreaInsets: useSafeAreaInsets
handleBackground: handleBackground,
draggableHeight: draggableHeight,
useSafeAreaInsets: useSafeAreaInsets,
stretchOnResize: stretchOnResize
)
transitioningDelegate = bottomSheetTransitioningDelegate
modalPresentationStyle = .custom
4 changes: 4 additions & 0 deletions Sources/BottomSheetPresentationController.swift
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ final class BottomSheetPresentationController: UIPresentationController {
private let startTargetIndex: Int
private let handleBackground: BottomSheetView.HandleBackground
private let useSafeAreaInsets: Bool
private let draggableHeight: CGFloat?
private let stretchOnResize: Bool
private var dismissVelocity: CGPoint = .zero
private var bottomSheetView: BottomSheetView?
@@ -63,12 +64,14 @@ final class BottomSheetPresentationController: UIPresentationController {
presentationDelegate: BottomSheetPresentationControllerDelegate?,
animationDelegate: BottomSheetViewAnimationDelegate?,
handleBackground: BottomSheetView.HandleBackground,
draggableHeight: CGFloat?,
useSafeAreaInsets: Bool,
stretchOnResize: Bool
) {
self.contentHeights = contentHeights
self.startTargetIndex = startTargetIndex
self.handleBackground = handleBackground
self.draggableHeight = draggableHeight
self.presentationDelegate = presentationDelegate
self.animationDelegate = animationDelegate
self.useSafeAreaInsets = useSafeAreaInsets
@@ -148,6 +151,7 @@ final class BottomSheetPresentationController: UIPresentationController {
contentView: presentedView,
contentHeights: contentHeights,
handleBackground: handleBackground,
draggableHeight: draggableHeight,
useSafeAreaInsets: useSafeAreaInsets,
stretchOnResize: stretchOnResize,
dismissalDelegate: self,
4 changes: 4 additions & 0 deletions Sources/BottomSheetTransitioningDelegate.swift
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ public final class BottomSheetTransitioningDelegate: NSObject {
public private(set) var contentHeights: [CGFloat]
private let startTargetIndex: Int
private let handleBackground: BottomSheetView.HandleBackground
private let draggableHeight: CGFloat?
private let useSafeAreaInsets: Bool
private let stretchOnResize: Bool
private var weakPresentationController: WeakRef<BottomSheetPresentationController>?
@@ -24,6 +25,7 @@ public final class BottomSheetTransitioningDelegate: NSObject {
contentHeights: [CGFloat],
startTargetIndex: Int = 0,
handleBackground: BottomSheetView.HandleBackground = .color(.clear),
draggableHeight: CGFloat? = nil,
presentationDelegate: BottomSheetPresentationControllerDelegate? = nil,
animationDelegate: BottomSheetViewAnimationDelegate? = nil,
useSafeAreaInsets: Bool = false,
@@ -32,6 +34,7 @@ public final class BottomSheetTransitioningDelegate: NSObject {
self.contentHeights = contentHeights
self.startTargetIndex = startTargetIndex
self.handleBackground = handleBackground
self.draggableHeight = draggableHeight
self.presentationDelegate = presentationDelegate
self.animationDelegate = animationDelegate
self.useSafeAreaInsets = useSafeAreaInsets
@@ -76,6 +79,7 @@ extension BottomSheetTransitioningDelegate: UIViewControllerTransitioningDelegat
presentationDelegate: presentationDelegate,
animationDelegate: animationDelegate,
handleBackground: handleBackground,
draggableHeight: draggableHeight,
useSafeAreaInsets: useSafeAreaInsets,
stretchOnResize: stretchOnResize
)
35 changes: 31 additions & 4 deletions Sources/BottomSheetView.swift
Original file line number Diff line number Diff line change
@@ -69,6 +69,8 @@ public final class BottomSheetView: UIView {
set { dimView.isHidden = newValue }
}

public let draggableHeight: CGFloat?

// MARK: - Private properties

private let useSafeAreaInsets: Bool
@@ -77,13 +79,16 @@ public final class BottomSheetView: UIView {
private let handleBackground: HandleBackground
private var topConstraint: NSLayoutConstraint!
private var targetOffsets = [CGFloat]()

private var initialOffset: CGFloat?
private var translationTargets = [TranslationTarget]()

private lazy var tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(tapGesture:)))
private lazy var panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(panGesture:)))
private lazy var springAnimator = SpringAnimator(dampingRatio: 0.8, frequencyResponse: 0.4)
private lazy var tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(tapGesture:)))

private lazy var panGesture: UIPanGestureRecognizer = {
let gestureRecognizer = PanGestureRecognizer(target: self, action: #selector(handlePan(panGesture:)))
gestureRecognizer.draggableHeight = draggableHeight
return gestureRecognizer
}()

private var bottomInset: CGFloat {
return useSafeAreaInsets ? .safeAreaBottomInset : 0
@@ -115,13 +120,15 @@ public final class BottomSheetView: UIView {
contentView: UIView,
contentHeights: [CGFloat],
handleBackground: HandleBackground = .color(.clear),
draggableHeight: CGFloat? = nil,
useSafeAreaInsets: Bool = false,
stretchOnResize: Bool = false,
dismissalDelegate: BottomSheetViewDismissalDelegate? = nil,
animationDelegate: BottomSheetViewAnimationDelegate? = nil
) {
self.contentView = contentView
self.handleBackground = handleBackground
self.draggableHeight = draggableHeight
self.contentHeights = contentHeights.isEmpty ? [.bottomSheetAutomatic] : contentHeights
self.useSafeAreaInsets = useSafeAreaInsets
self.stretchOnResize = stretchOnResize
@@ -418,6 +425,26 @@ public final class BottomSheetView: UIView {
}
}

// MARK: - Private types

private class PanGestureRecognizer: UIPanGestureRecognizer {
var draggableHeight: CGFloat?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
guard let firstTouch = touches.first, let view = view, let draggableHeight = draggableHeight else {
return super.touchesBegan(touches, with: event)
}

let height = CGFloat.handleHeight + draggableHeight
let touchPoint = firstTouch.location(in: view)
let draggableRect = CGRect(x: 0, y: 0, width: view.frame.width, height: height)

if draggableRect.contains(touchPoint) {
super.touchesBegan(touches, with: event)
}
}
}

// MARK: - Private extensions

private extension UIColor {

0 comments on commit aab1f2c

Please sign in to comment.