MBProgressHUD 二次封裝

針對項目的需求做了一下二次封裝,主要是針對自己的項目去做的封裝。特此留存一下以備后期使用

import UIKit
import Foundation
enum HudProgressMode {
    case text
    case circle
}

class HudOffset {
    private static let SCALE = UIScreen.main.bounds.size.width / 768
    private static let HEIGHT = UIScreen.main.bounds.size.height
    
    static let loadingOffset = CGPoint(x: 120 * SCALE, y: 0)
    static let textOffset = CGPoint(x: 120, y: HEIGHT / 2.0 - 70 * SCALE)
    static let text = CGPoint(x: 0, y: HEIGHT / 2.0 - 70 * SCALE)
}

/// HUD需要用到的一些顏色
class HudBaseInfo {
    static let loading_bezelViewBgColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
    static let loading_contentColor =  UIColor.white
    
    
    static let text_bezelViewBgColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
    static let text_contentColor =  UIColor.white
    
    
    static let detailsLabelFont = UIFont.systemFont(ofSize: 16)
    static let contentMargin : CGFloat = 20
}

class SHud: NSObject {
    private var hud : MBProgressHUD? = nil
    private static let instance = SHud.init()
    
    
    /// 顯示純文字
    /// - Parameters:
    ///   - msg: 文字
    ///   - inView: 在哪個view中顯示
    ///   - isOffset: 是否需要設(shè)置content內(nèi)容的偏移
    ///   - offset: 偏移
    static func showMsg(_ msg : String = "" , inView : UIView = UIApplication.shared.windows.first! , isOffset : Bool = false , offset : CGPoint? = nil){
        _initHud(msg: msg, inView: inView, type: .text, isOffset: isOffset, offset: offset)
        instance.hud?.hide(animated: true, afterDelay: 1.5)
    }
    
    /// 圓環(huán)加載動畫
    /// - Parameters:
    ///   - msg: 文字
    ///   - inView: 在哪個view中顯示
    ///   - isOffset: 是否需要設(shè)置content內(nèi)容的偏移
    ///   - offset: 偏移
    static func showLoading(_ msg : String = "" , inView : UIView = UIApplication.shared.windows.first! , isOffset : Bool = false , offset : CGPoint? = nil){
        _initHud(msg: msg, inView: inView, type: .circle , isOffset: isOffset, offset: offset)
        
        //自定義圓環(huán)動畫
        let circleImgView = UIImageView.init()
        let animation = CABasicAnimation.init(keyPath: "transform.rotation")
        
        circleImgView.image = UIImage.init(named: "hudLoader")
        animation.toValue = Double.pi * 2
        animation.duration = 1
        animation.repeatCount = MAXFLOAT
        
        circleImgView.layer.add(animation, forKey: nil)
        instance.hud?.customView = circleImgView
    }
    
    
    
    
    /// 初始化MBProgressHUD
    /// - Parameters:
    ///   - msg: 文本內(nèi)容
    ///   - inView: 添加到哪個View中
    ///   - type: 需要創(chuàng)建什么類型的數(shù)據(jù)
    ///   - isOffset: 是否需要設(shè)置內(nèi)容層的偏移
    ///   - offset: 偏移位置
    private static func _initHud(msg : String , inView : UIView , type : HudProgressMode , isOffset : Bool , offset : CGPoint?){
        //判斷hud是否已設(shè)置為nil
        if instance.hud != nil {
            instance.hud?.hide(animated: true)
            instance.hud = nil
        }
        instance.hud = MBProgressHUD.showAdded(to: inView, animated: true)
        instance.hud?.detailsLabel.text = msg
        //設(shè)置顯示內(nèi)容的樣式
        instance.hud?.bezelView.style = .solidColor
        
        switch type {
            case .circle:
                instance.hud?.bezelView.backgroundColor = HudBaseInfo.loading_bezelViewBgColor
                instance.hud?.contentColor = HudBaseInfo.loading_contentColor
            case .text:
                instance.hud?.bezelView.backgroundColor = HudBaseInfo.text_bezelViewBgColor
                instance.hud?.contentColor = HudBaseInfo.text_contentColor
        }
        
        instance.hud?.detailsLabel.font = HudBaseInfo.detailsLabelFont
        instance.hud?.margin = HudBaseInfo.contentMargin
        //默認隱藏后從父控制器中移除
        instance.hud?.removeFromSuperViewOnHide = true
        //設(shè)置偏移
        
        //文本默認顯示在底部
        if type == .text {
            instance.hud?.offset = HudOffset.text
        }
        
        if offset != nil {//如果傳遞了offset的值則默認使用offset的值
            instance.hud?.offset = offset!
        }else if isOffset{//如果是設(shè)置了偏移
            if type == .text {
                instance.hud?.offset = HudOffset.textOffset
            }else{
                instance.hud?.offset = HudOffset.loadingOffset
            }
        }
        
        //設(shè)置內(nèi)容層的最小尺寸
        instance.hud?.minSize = CGSize(width: 40, height: 40)
        switch type {
            case .circle:
                instance.hud?.mode = .customView
            case .text:
                instance.hud?.mode = .text
        }
    }
    
    /// 隱藏hud加載動畫
    static func dismiss(){
        if instance.hud != nil {
            instance.hud?.hide(animated: true)
        }
        instance.hud = nil
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容