DrawerView
DrawerView copied to clipboard
Added a "5th" Intermediate Pushed Up Position
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
}
}
// . . .