Contents

william-weng/wwhud

- [Custom read animation, support custom pictures, GIF animation.](https://youtu.be/6XVxvRKoAHM)

[Introduction - 簡介](https://swiftpackageindex.com/William-Weng)

https://github.com/user-attachments/assets/72f5d179-59a6-45eb-88db-1027e24a5a07

Installation with Swift Package Manager

dependencies: [
    .package(url: "https://github.com/William-Weng/WWHUD.git", .upToNextMajor(from: "1.6.0"))
]

Function - 可用函式

|函式|功能| |-|-| |display(effect:height:backgroundColor:)|顯示HUD動畫| |dismiss(animation:options:completion:)|移除HUD顯示| |flash(effect:height:backgroundColor:animation:options:completion:)|顯示一段時間的HUD動畫,然後會移除| |updateProgess(text:font:textColor:)|更新進度文字及字型| |closeLabelSetting(title:isHidden:font:)|強制關閉Label的顯示相關設定|

WWHUD.Delegate

|函式|功能| |-|-| |forceClose(hud:)|強制關閉HUD|

Example

import UIKit
import WWHUD

final class ViewController: UIViewController {
    
    private var timer: CADisplayLink?
    private var percentage: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        WWHUD.shared.delegate = self
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        WWHUD.shared.flash(animation: 2.0)
    }
}

extension ViewController: WWHUD.Delegate {
    
    func forceClose(hud: WWHUD) {
        percentage = 0
        cleanTimer()
    }
}

private extension ViewController {
    
    @IBAction func displaySVGHUD(_ sender: UIBarButtonItem) {
        
        let svg = """
        <svg viewBox="0 0 160 160" width="160" height="160">
            <circle cx="80" cy="80" r="50" fill="#67AAF9" />
            <g transform="matrix(0.866, -0.5, 0.25, 0.433, 80, 80)">
                <path d="M 0,70 A 65,70 0 0,0 65,0 5,5 0 0,1 75,0 75,70 0 0,1 0,70Z" fill="#F00">
                    <animateTransform attributeName="transform" type="rotate" from="360 0 0" to="0 0 0" dur="1s" repeatCount="indefinite" />
                </path>
            </g>
            <path d="M 50,0 A 50,50 0 0,0 -50,0Z" transform="matrix(0.866, -0.5, 0.5, 0.866, 80, 80)" fill="#67AAF9"/>
        </svg>
        """
        
        WWHUD.shared.flash(effect: .svg(svg: svg), height: 256.0, animation: 2.5)
    }
        
    @IBAction func displayImageHUD(_ sender: UIBarButtonItem) {
        
        let image = #imageLiteral(resourceName: "Crab")
        
        updateProgressPercentage(selector: #selector(updateProgressForHUD))
        WWHUD.shared.display(effect: .shake(image: image, angle: 10.0, duration: 1.0), height: 128, backgroundColor: .green.withAlphaComponent(0.3))
    }
    
    @IBAction func displayGifHUD(_ sender: UIBarButtonItem) {
        
        guard let gifUrl = Bundle.main.url(forResource: "SeeYou", withExtension: ".gif") else { return }
        
        updateProgressPercentage(selector: #selector(updateProgressForGifHUD))
        WWHUD.shared.display(effect: .gif(url: gifUrl, options: nil), height: 256.0, backgroundColor: .black.withAlphaComponent(0.3))
    }
    
    @IBAction func flashHUD(_ sender: UIBarButtonItem) {
        
        let image = #imageLiteral(resourceName: "White")
        
        WWHUD.shared.flash(effect: .indicator(image: image, count: 8, size: CGSize(width: 20, height: 40), duration: 1.0, cornerRadius: 20, backgroundColor: .yellow), height: 200, backgroundColor: .black.withAlphaComponent(0.3), animation: 2.0) { postion in
            print(postion)
        }
    }
}

private extension ViewController {
    
    @objc func updateProgressForHUD(_ sender: CADisplayLink) {
        
        let percentageText = "\(percentage) %"
        
        WWHUD.shared.updateProgess(text: percentageText)
        percentage += 1
        
        if (percentage > 100) { dismissHUD() }
    }
    
    @objc func updateProgressForGifHUD(_ sender: CADisplayLink) {
        
        var percentageText = "努力下載中…"
        var percentageTextColor: UIColor = .clear

        switch percentage {
        case ...30:
            percentageText = "努力下載中…"
            percentageTextColor = .red
        case 31...60:
            percentageText = "下載快一半…"
            percentageTextColor = .yellow
        case 61...90:
            percentageText = "就快下載完成了…"
            percentageTextColor = .green
            WWHUD.shared.closeLabelSetting(title: "關閉")
        case 90...99:
            percentageText = "還差一點點…"
            percentageTextColor = .blue
        case 100...:
            percentageText = "終於下載完成了…"
            percentageTextColor = .white
            dismissHUD()
        default: percentageText = ""
        }
        
        percentage += 1
        WWHUD.shared.updateProgess(text: percentageText, textColor: percentageTextColor)
    }
}

// MARK: - 小工具
private extension ViewController {
    
    func cleanTimer() {
        timer?.invalidate()
        timer = nil
    }
    
    func dismissHUD() {
        cleanTimer()
        percentage = 0
        WWHUD.shared.dismiss { _ in WWHUD.shared.updateProgess(text: nil) }
    }
    
    func updateProgressPercentage(selector: Selector) {
        timer?.invalidate()
        timer = nil
        timer = CADisplayLink(target: self, selector: selector)
        timer?.preferredFramesPerSecond = 24
        timer?._fire()
    }
}

Package Metadata

Repository: william-weng/wwhud

Default branch: main

README: README.md