Giter VIP home page Giter VIP logo

slideovercard's Introduction

SlideOverCard Project logo

Twitter: @joogps

A SwiftUI card design similar to the one used by Apple in HomeKit, AirPods, Apple Card and AirTag setup, NFC scanning, Wi-Fi password sharing and more. It is specially great for setup interactions.


Clear Spaces demo QR code scanner demo Example preview demo


From left to right: SlideOverCard being used in Clear Spaces, a QR code scanner prompt (made with CodeScanner) and a sample demo app

Installation

This repository is a Swift package, so just include it in your Xcode project and target under File > Add package dependencies. Then, import SlideOverCard to the Swift files where you'll be using it.

Note

If your app runs on iOS 13, you might find a problem with keyboard responsiveness in your layout. That's caused by a SwiftUI limitation, unfortunately, since the ignoresSafeArea modifier was only introduced for the SwiftUI framework in the iOS 14 update.

Usage

Adding a card to your app is insanely easy. Just add a .slideOverCard() modifier anywhere in your view hierarchy, similarly to a .sheet():

.slideOverCard(isPresented: $isPresented) {
  // Here goes your awesome content
}

And that's it! It just works. In this case, $isPresented is a boolean binding. This way you can dismiss the view anytime by setting it to false.

Extra steps

Customization

The default .slideOverCard() modifier will have a transition, drag and tap controls and a dismiss button set by default. You can override this by manually setting the SOCOptions option set:

// This creates a card that can be dragged, but not dismissed by dragging
.slideOverCard(isPresented: $isPresented, options: [.disableDragToDismiss]) {
}

// This creates a card that can't be dragged nor dismissed by dragging
.slideOverCard(isPresented: $isPresented, options: [.disableDrag, .disableDragToDismiss]) {
}

// This creates a card that can't be dismissed by an outside tap
.slideOverCard(isPresented: $isPresented, options: [.disableTapToDismiss]) {
}

// This creates a card with no dismiss button
.slideOverCard(isPresented: $isPresented, options: [.hideDismissButton]) {
}

If you want to change styling attributes of the card, such as the corner size, the corner style, the inner and outer paddings, the dimming opacity and the shape fill style, such as a gradient, just specify a custom SOCStyle struct.

.slideOverCard(isPresented: $isPresented, style: SOCStyle(cornerRadius: 24.0,
                                                          continuous: false,
                                                          innerPadding: 16.0,
                                                          outerPadding: 4.0,
                                                          dimmingOpacity: 0.1,
                                                          style: .black)) {
}

In case you want to execute code when the view is dismissed (either by the exit button or drag controls), you can also set an optional onDismiss closure parameter:

// This card will print some text when dismissed
.slideOverCard(isPresented: $isPresented, onDismiss: {
    print("I was dismissed.")
}) {
    // Here goes your amazing layout
}

Alternatively, you can add the card using a binding to an optional identifiable object. That will automatically animate the card between screen changes.

// This uses a binding to an optional object in a switch statement
.slideOverCard(item: $activeCard) { item in
    switch item {
        case .welcomeView:
            WelcomeView()
        case .loginView:
            LoginView()
        default:
            ..........
    }
}
Accessory views

This package also includes a few accessory views to enhance your card layout. The first one is the SOCActionButton() button style, which can be applied to any button to give it a default "primary action" look, based on the app's accent color. The SOCAlternativeButton() style will reproduce the same design, but with gray. And SOCEmptyButton() will create a text-only button. You can use them like this:

Button("Do something") {
  ...
}.buttonStyle(SOCActionButton()) // Use the modifier of your choice

There's also the SOCDismissButton() view. This view will create the default dismiss button icon used for the card (based on https://github.com/joogps/ExitButton).

Demo

You can check out a demo project for this package in the demo branch of this repository.

slideovercard's People

Contributors

joogps avatar kiecooboi avatar kleemann avatar rminsh avatar viere1234 avatar wouter01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slideovercard's Issues

Close on Tap Outside

Is there a way you can make it, so a tap outside of the card dismisses it. Thanks!

Sheet with .slideOverCard(item...) modifier can't be opened the second time

Steps to repro

  1. Open the sheet
  2. Close it
  3. Try to open again -> the UI is not clickable. Seems like the Window is not dismissed properly

Env
Simulator iPhone 15 pro max, iOS 17.4

When I use .slideOverCard(isPresented...) modifier everything works fine

Code sample

struct Sheet3: Identifiable {
    var id: String { title }
    let title: String
}

struct ContentView: View {
    
    @State private var showSheet3: Sheet3? = nil
    
    var body: some View {
        VStack {
            // Can't click again once dismissed
            Button("Show sheet #3") { showSheet3 = .init(title: "Sheet #3")  }
        }
        .slideOverCard(item: $showSheet3, onDismiss: { print("Sheet #3 dismiss") }) { sheet3 in
            Text(sheet3.title)
        }
    }
}

Video

When I dismissed the sheet I was trying to click on a Button again.

Simulator.Screen.Recording.-.iPhone.15.Pro.Max.-.2024-04-11.at.14.31.41.mp4

Documentation need to be updated for Xcode 13?

This might be actually my limited knowledge on how to use Swift Packages but the documentation says that I'm supposed to go to File > Swift Package Manager.

I don't see such menu item so I was trying to do it another way but still can't make it work:

2022-01-06 17 34 06

Any hints on how to link this package?

How to manually add to a project?

Hi,

I would like to use this in Swift Playgrounds? I was thinking that the best way to do this was copy and paste the source files, but I'm not sure which ones to copy and to where?

Thanks

Sheet onDismiss is called immediately when View appeared

Steps to repro

Case 1

  1. Attach .slideOverCard(item: ...) modifier to a View
  2. Implement onDismiss closure
  3. Closure is executed 5 times when sheet is not opened

Case 2

  1. Attach .slideOverCard(isPresented: ...) modifier to a View
  2. Implement onDismiss closure
  3. Closure is executed 2 times when sheet is not opened

Env
Simulator iPhone 15 pro max, iOS 17.4

Code Sample 1

struct Sheet3: Identifiable {
    var id: String { title }
    let title: String
}

struct ContentView: View {
    
    @State private var showSheet3: Sheet3? = nil
    
    var body: some View {
        VStack {
            Button("Show sheet #3") { showSheet3 = .init(title: "Sheet #3")  }
        }
        .slideOverCard(item: $showSheet3, onDismiss: { print("Sheet #3 dismiss") }) { sheet3 in
            Text(sheet3.title)
        }
    }
}

Console Output 1

Sheet #3 dismiss
Sheet #3 dismiss
Sheet #3 dismiss
Sheet #3 dismiss
Sheet #3 dismiss

Code Sample 2

struct ContentView: View {
    
    @State private var showSheet1 = false
    
    var body: some View {
        VStack {
            Button("Show sheet #1") { showSheet1 = true }
        }
        .slideOverCard(isPresented: $showSheet1, onDismiss: { print("Sheet #1 dismiss") }) {
            Text("Sheet #1")
        }
    }
}

Console Output 2

Sheet #1 dismiss
Sheet #1 dismiss

Error with `.slideOverCard`

iOS 15.2

Xcode Version 13.2.1 (13C100)

My ContentView.swift:

import SwiftUI

struct ContentView: View {
    
    @State private var showCard = false
    var body: some View {
        HStack{
            Text("Hello, world!")
                .padding()
            Button("abb"){
                showCard = true
            }
        }
        .slideOverCard(isPresented: $showCard ) {
            Text("Hello, world!")
              // Here goes your awesome content
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

And the error:

๐Ÿ”ด Value of type 'HStack<TupleView<(some View, Button<Text>)>>' has no member 'slideOverCard'
  • If I use .sheet, no error occurs. Why?
import SwiftUI

struct ContentView: View {
    
    @State private var showCard = false
    var body: some View {
        HStack{
            Text("Hello, world!")
                .padding()
            Button("abb"){
                showCard = true
            }
        }
        .sheet(isPresented: $showCard ) {
            Text("Hello, world!")
              // Here goes your awesome content
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

SOCManager doesn't work inside sheet

If I tried to present a card using the SOCManager from a Sheet, it doesn't work.

Sample Code:

struct ContentView: View {
    
    @State private var isShowingCard: Bool = false
    @State private var isShowingSheet: Bool = false
    
    var body: some View {
        
        NavigationView {
            VStack {
                Button("Show Sheet") {
                    isShowingSheet.toggle()
                }
            }
            .navigationTitle("Card Test")
            .sheet(isPresented: $isShowingSheet, content: {
                Button("Show Card") {
                    SOCManager.present(isPresented: $isShowingCard) {
                        ZStack {
                            Text("Card")
                        }
                    }
                }
            })
        }
    }
}

Error message in terminal:

2021-06-02 14:54:58.677044+0300 CardTest[53318:3531446] [Presentation] Attempt to present <TtGC7SwiftUI19UIHostingControllerGV13SlideOverCard13SlideOverCardGVS_6ZStackVS_4Text__: 0x7faab5d29920> on <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x7faab5f05810> (from <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x7faab5f05810>) which is already presenting <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x7faab5d12080>.

Sheet with innerPadding set to 0 still has some paddings set

Steps to repro

  1. Set sheet's style with innerPadding = 0
  2. Open the sheet

Env
Simulator iPhone 15 pro max, iOS 17.4

Seems like you still provide some paddings to make sure content isn't displayed outside the corners.
I think the expected behavior is that there is no padding and the content is clipped by shape with a corner radius provided

Simulator Screenshot - iPhone 15 Pro Max - 2024-04-11 at 14 44 43

SOCManager present animation speed

Version: SlideOverCard 2.0.0, Xcode 13.3
Issue: Unnaturally fast animation when displaying a card using SOCManager.present vs .slideOverCard(isPresented.

I would expect both versions to display the card at the same animation speed. The SOCManager presents the card so quickly that it feels off.

Potential Quick Fix:
Give the SOCManager the same animation speed as the slideOverCard

Potential Enhancement:
Add and option to change the speeds between slow, medium, fast, and or passing in custom animation speeds

Code snippets:

Button {
    showLogIn = true

    SOCManager.present(isPresented: $showLogIn, onDismiss: {
       // code
    }, options: [.disableDrag, .disableDragToDismiss, .hideExitButton]) {
      SomeView(isPresented: $showLogIn)
    }
  }
}
.slideOverCard(isPresented: $showView, options: [.disableDrag, .disableDragToDismiss, .hideExitButton]) {
  SomeView(isPresented: $showSummary)
}

Thank you for your time.

Bible

// This creates a card that can be dragged, but not dismissed by dragging
.slideOverCard(isPresented: $isPresented, options: [.disableDragToDismiss]) {
}

// This creates a card that can't be dragged or dismissed by dragging
.slideOverCard(isPresented: $isPresented, options: [.disableDrag, .disableDragToDismiss]) {
}

// This creates a card with no exit button
.slideOverCard(isPresented: $isPresented, options: [.hideExitButton]) {
}

No padding around card

Hi, I have iOS 15 and iPhone 13 Pro, the card that appears on my screen touches the edges of the screen and has no padding on left, right and bottom.

Can I use multiple slideOverCard modifiers across the project?

  1. Right now only the first modifier works for me. When I close the SlideOverCard I can't open it again
  2. Is it possible to overlay SlideOverCard on top of each other? For example I open SlideOverCard A from the screen, but A also contains SlideOverCard B which should be opened on top

Card hidden behind tab bar.

When using a tab bar, and showing a SlideOverCard on a subview, the card is hidden behind the tab bar.
Is there any way that the y offset could take into account the height of the tab bar?

Code to reproduce:

import SwiftUI
import SlideOverCard

struct ContentView: View {
    @State var tab: Int = 0
    
    
    var body: some View {
        TabView(selection: $tab) {
            View1()
                .tabItem { Image(systemName: "rectangle") }
                .tag(0)
            View2()
                .tag(1)
                .tabItem { Image(systemName: "rectangle") }
        }
    }
}

struct View1: View {
    var body: some View {
        Text("View 1")
    }
}
struct View2: View {
    @State var showCard: Bool = false
    
    var body: some View {
        ZStack {
            VStack {
                Text("View 2")
                    .font(.largeTitle)
                Button(action: { self.showCard = true }) {
                    Text("Show card!")
                }
                ScrollView() {
                    ForEach(0..<100) { i in
                        Text("\(i)")
                            .font(.caption)
                    }
                }
            }
        }
        .slideOverCard(isPresented: $showCard) {
            VStack {
                Image(systemName: "clock")
                    .font(.largeTitle)
                Text("Card is showing, but is cut off ...")
                    .font(.subheadline)
            }
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Add support for multiple items

Add support so that the view can be called with multiple items such as

        .sheet(item: $activeSheet) { item in
            switch item {
            case .photoPicker:
                ImagePicker(selectedImage: $imageBuffer, sourceType: .photoLibrary)
            case ........

Issue with Xcode 13 beta 3

UIApplication.shared.windows.first?.rootViewController?.present(controller, animated: false)
        DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {

'shared' is unavailable in application extensions for iOS: Use view controller based solutions where appropriate instead.

iOS 13 Support?

Hi, thanks for this great work! I think the iOS 14 requirement is unnecessary for this, you can easily integrate this for iOS 13 too. This is just a suggestion and it's important I think :)

Anyway, thanks again :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.