mkko / DrawerView

A drop-in view, to be used as a drawer anywhere in your app
MIT License
375 stars 57 forks source link

Added a "5th" Intermediate Pushed Up Position #55

Open anomaddev opened 1 year ago

anomaddev commented 1 year ago

Had a use case where I needed a collapsed case, a partiallyOpen case, and then a pushedUp case that was basically a partially open state that opens a little more to handle a view that animates in. Added a new snapPosition pushedUp and a variable on the drawer called pushUp that will take the partiallyOpenHeight and open it the set amount more to add to the height without fully opening the drawer.

See example:

@objc public enum DrawerPosition: Int {
    case closed = 0
    case collapsed = 1
    case partiallyOpen = 2
    case pushedUp = 3
    case open = 4
}
@IBDesignable open class DrawerView: UIView {
// . . .
/// The height of the drawer when partially open.
    public var partiallyOpenHeight: CGFloat = 264.0 {
        didSet {
            self.updateSnapPosition(animated: false)
        }
    }

    /// The amount the drawer is pushed up from partially open when selected.
    public var pushUp: CGFloat = 50 {
        didSet {
            self.updateSnapPosition(animated: false)
        }
    }
// . . . 
fileprivate func snapPosition(for position: DrawerPosition, inSuperView superview: UIView) -> CGFloat {
        switch position {
        case .open:
            if let height = self.openHeight {
                return max(self.topMargin, superview.bounds.height - bottomInset - height)
            } else {
                return self.topMargin
            }

        case .pushedUp:
            return superview.bounds.height - bottomInset - (self.pushUp + self.partiallyOpenHeight)
        case .partiallyOpen:
            return superview.bounds.height - bottomInset - self.partiallyOpenHeight
        case .collapsed:
            return superview.bounds.height - bottomInset - self.collapsedHeight
        case .closed:
            // When closed, the safe area is ignored since the
            // drawer should not be visible.
            return superview.bounds.height
        }
    }
// . . .