Drawer

0.0.3

A SwiftUI bottom-up controller, like in the Maps app. Drag to expand or minimize.
maustinstar/swiftui-drawer

What's New

Declarative View Modifiers

2020-07-16T17:55:05Z

🥳 New Declarative View Modifiers!

Thank you for all your support and feature requests!

New declarative view modifiers will help you customize the drawer experience. Shoutout to u/pupdogg007 for my favorite requested feature: locking the drawer.

I also spent a lot of time refining the fluidity of the drawer animations. In v0.0.2, I made the drags more responsive by toggling animation. In v0.03, after formulating a function on Desmos, I am introducing 'springiness' to the drawers when they are pulled beyond their boundaries. This animation feels more fluid by asymptotically reducing the influence of a drag.

🔒 Locked

Locks the drawer in a controlled position. When set to true, the drawer will animate into the locked height.

Lock into the current resting height

/*Drawer*/.locked($locked) { (currentPosition) in
    return currentPosition
}

🪀 Spring

Sets the springiness of the drawer when pulled past boundaries.

The user's drag displacement is transformed by a logistic curve for a natural hard-spring pull that reaches an asymptote.

/*Drawer*/.spring(20)

😴 OnRest

A callback to receive updates when the drawer reaches a new resting level. This closure is executed every time the drawer reaches a new resting hieght. Use this when you want to receive updates on the drawer's changes.

/*Drawer*/.onRest { (restingHeight) in
    print(restingHeight)
}

💥 Impact

Sets the haptic impact of the drawer when resting

/*Drawer*/.impact(.light)

Other Notes

Deprecated the init's impact parameter to prefer the declarative modifier.

SwiftUI Drawer

A SwiftUI bottom-up controller, like in the Maps app. Drag to expand or minimize.

Contents

Package

Add a dependency in your your Package.swift

.package(url: "https://github.com/maustinstar/swiftui-drawer.git", from: "0.0.3"),

Usage

Embed your view content in a ZStack with the Drawer as the last element. The heights parameter defines a list of resting heights for the drawer.

ZStack {

    ScrollView {
        //...
    }
    
    Drawer(heights: [100, 340]) {
        Color.blue
    }
}

View Modifiers

Following SwiftUI's declarative syntax, these view modifiers return a modified Drawer.

Impact

💥 impact(_: UIImpactFeedbackGenerator.FeedbackStyle) -> Drawer

Sets the haptic impact of the drawer when resting.

Feedback Style Choose from the possible impact styles. Apple Docs

public enum FeedbackStyle : Int {

    /// A collision between small, light user interface elements.
    case light = 0
    
    /// A collision between moderately sized user interface elements.
    case medium = 1
    
    /// A collision between large, heavy user interface elements.
    case heavy = 2
    
    @available(iOS 13.0, *)
    case soft = 3
    
    @available(iOS 13.0, *)
    case rigid = 4
}

Usage

Drawer(heights: [100, 340]) {
    Color.blue
}.impact(.light)

Spring

🪀 spring(_: CGFloat) -> Drawer

Sets the springiness of the drawer when pulled past boundaries.

Spring Level A positive number, in pixels, to define how far the drawer can be streched beyond bounds.

A spring of 0 will not let the user drag the drawer beyond the bound of the minimum and maximum heights.

A positive spring level will allow users to pull the drawer a specified number of pixels beyond the bounds. The user's drag displacement is transformed by a logistic curve for a natural hard-spring pull that reaches an asymptote.

Default is 12px.

Usage Set a spring distance

Drawer(heights: [100, 340]) {
    Color.blue
}.spring(20)

Toggle a spring distance

Drawer(heights: [100, 340]) {
    Color.blue
}.spring(isSpringy ? 0 : 20)

Locked

🔒 locked(_: Binding<Bool>, to height: @escaping (_ restingHeight: CGFloat) -> CGFloat) -> Drawer

Locks the drawer in a controlled position. When set to true, the drawer will animate into the locked height.

isLocked A Binding Bool indicating if the drawer should be locked.

height A closure returning the height to lock the drawer. The closure's argument is the drawer's current resting height.

Usage Lock into a fixed position.

Drawer(heights: [100, 340]) {
    Color.blue
}.locked($locked) {_ in
    return 30
}

Lock into the current resting height

Drawer(heights: [100, 340]) {
    Color.blue
}.locked($locked) { (current) in
    return current
}

OnRest

😴 onRest(_: @escaping (_ height: CGFloat) -> ()) -> Drawer

A callback to receive updates when the drawer reaches a new resting level.

Closure This closure is executed every time the drawer reaches a new resting hieght. Use this when you want to receive updates on the drawer's changes.

Usage Lock into a fixed position.

Drawer(heights: [100, 340]) {
    Color.blue
}.onRest { (restingHeight) in
    print(restingHeight)
}

Example 1

A multi-height drawer with haptic impact.

Drawer(heights: [100, 340, UIScreen.main.bounds.height - 40], impact: .light) {
    ZStack {
        
        RoundedRectangle(cornerRadius: 12)
            .foregroundColor(.white)
            .shadow(radius: 100)
        
        VStack(alignment: .center) {
            Spacer().frame(height: 4.0)
            RoundedRectangle(cornerRadius: 3.0)
                .foregroundColor(.gray)
                .frame(width: 30.0, height: 6.0)
            Spacer()
        }
    }
}

Example 2

A basic two-height drawer.

Drawer(heights: [100, 340]) {
    Color.blue
}

Authors

Description

  • Swift Tools 5.2.0

Dependencies

  • None
Last updated: Sat Aug 15 2020 14:24:12 GMT-0500 (GMT-05:00)