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: 是否需要设置content内容的偏移
    ///   - 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)
    }
    
    /// 圆环加载动画
    /// - Parameters:
    ///   - msg: 文字
    ///   - inView: 在哪个view中显示
    ///   - isOffset: 是否需要设置content内容的偏移
    ///   - 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)
        
        //自定义圆环动画
        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: 文本内容
    ///   - inView: 添加到哪个View中
    ///   - type: 需要创建什么类型的数据
    ///   - isOffset: 是否需要设置内容层的偏移
    ///   - offset: 偏移位置
    private static func _initHud(msg : String , inView : UIView , type : HudProgressMode , isOffset : Bool , offset : CGPoint?){
        //判断hud是否已设置为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
        //设置显示内容的样式
        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
        //设置偏移
        
        //文本默认显示在底部
        if type == .text {
            instance.hud?.offset = HudOffset.text
        }
        
        if offset != nil {//如果传递了offset的值则默认使用offset的值
            instance.hud?.offset = offset!
        }else if isOffset{//如果是设置了偏移
            if type == .text {
                instance.hud?.offset = HudOffset.textOffset
            }else{
                instance.hud?.offset = HudOffset.loadingOffset
            }
        }
        
        //设置内容层的最小尺寸
        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
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容