Skip to content
/ HUD Public

SwiftUI实现的弹窗HUD,支持Loading,Progress,Success,Fail,Toast,Popup,支持自定义View

License

Notifications You must be signed in to change notification settings

zjinhu/HUD

Repository files navigation

SPM Xcode 14.2+ iOS 13.0+ SwiftUI 3.0+

HUD is a Loading pop-up tool developed based on SwiftUI, style reference ProgressHUD and PopupView.

The current functions include Loading, Progress, Success, Fail, and PopupView, which support custom styles inherited from protocols and pop-up animations.

Support dark mode

Usage

Add it on the page that needs to use Loading or pop-up window (only cover the current page, if you need to cover TabView or NavigationView, please add it directly on the root View, such as ContentView())

//If UIViewController uses UIHostController, you don't need to add
.addHUD()

When your SwiftUI uses UIHostingController and adds it to UITabBarController or UINavigationController, the pop-up window using SwiftUI cannot cover UITabBar and UINavigationBar, so the HUD will judge the top-level VC when the show is executed, and then judge which kind of covering to perform Solution, perfectly cover the UIKit-level navigation bar, as shown below:

For specific usage and effects, see Demo

Various built-in pop-up windows

    @State var loading = LoadingView(text: .constant(nil))

    @State var loadingText = LoadingView(text: .constant("loading...")@State var fail = FailView(text: .constant(nil))
    @State var succ = SuccessView(text: .constant(""))

Text and Progress need to bind external parameters, so they can be used like this

    @State var progress: CGFloat = 0
    @State var progressView: StepView?

    @State var loadingText: String?
    @State var loading: LoadingView?

    //bind in place
    .onAppear {
        progressView = StepView(progress: $progress)
        loading = LoadingView(text: $loadingText)
    }
    //Modify the current progress or loadingText and then it can change automatically

The rest only needs to be in the trigger position

   Button {
       loading.show()
   } label: {
       Text("Loading Short Text")
   }

or according to state control

  .onChange(of: revenueCat.isPurchasing) { newValue in
       if newValue{
            loading.show()
       }else{
            loading.dismiss()
       }
   }

close HUD

.dismiss()

There are many custom functions in the adapter

    // Do you need a mask
     var needMask: Bool = true
     // popup window background color
     var backgroundColour: Color = .clear
     //The pop-up window ignores the safe area
     var ignoresSafeArea: Bool = false
     //Click outside the area to close the popup window
     var touchOutsideToDismiss: Bool = false
     // rounded corner radian
     var cornerRadius: CGFloat = 10
     // Gesture off
     var dragGestureProgressToClose: CGFloat = 1/3
     // Gesture close animation
     var dragGestureAnimation: Animation = .interactiveSpring()
    
     // popup window background shadow color
     var shadowColour: Color = .black.opacity(0.2)
     var shadowRadius: CGFloat = 5
     var shadowOffsetX: CGFloat = 0
     var shadowOffsetY: CGFloat = 0
    
     //The padding from the top, the default is 0, Top Popup will use it
     var topPadding: CGFloat = 0
     //The padding from the bottom, the default is 0, Bottom Popup will use it
     var bottomPadding: CGFloat = 0
     //Bottom PopupView automatically adds the height of the safe area
     var bottomAutoHeight: Bool = false
    
    
     //horizontal padding, the default is 0, most cases will be used by Center Popup
     var horizontalPadding: CGFloat = 0
     //The execution time of pop-up animation in the middle
     var centerAnimationTime: CGFloat = 0.1
     //Center PopupView pop-up animation ratio
     var centerTransitionExitScale: CGFloat = 0.86
     //Center PopupView pop-up animation ratio
     var centerTransitionEntryScale: CGFloat = 1.1
    
    
     // popup animation
     var transitionAnimation: Animation = .spring(response: 0.32, dampingFraction: 1, blendDuration: 0.32)

     //Stack style--exposed position--default 6
     var stackViewsOffset: CGFloat = 6
     //stack style -- scale
     var stackViewsScale: CGFloat = 0.06
     //stack style - rounded corners
     var stackViewsCornerRadius: CGFloat = 10
     //stack style--maximum heap display quantity
     var maxStackCount: Int = 3
    
    
     // Whether to automatically close
     var autoDismiss: Bool = false
     //automatic shutdown waiting time
     var autoDismissTime: TimeInterval = 3

Please refer to Demo for the specific use of the code api and detailed effects

Install

Cocoapods

  1. Add pod 'SwiftUIHUD' in Podfile

  2. Execute pod install or pod update

  3. Import import SwiftUIHUD

Swift Package Manager

Starting from Xcode 11, the Swift Package Manager is integrated, which is very convenient to use. HUD also supports integration via Swift Package Manager.

Select File > Swift Packages > Add Pacakage Dependency in Xcode's menu bar, and enter in the search bar

https://github.com/jackiehu/HUD, you can complete the integration

More tools to speed up APP development

ReadMe Card

[ReadMe Card](jackiehu/Toast_SwiftUI (github.com))

ReadMe Card

ReadMe Card

ReadMe Card

About

SwiftUI实现的弹窗HUD,支持Loading,Progress,Success,Fail,Toast,Popup,支持自定义View

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages