Swift3.0 MBProgressHUD的封装使用

创建 UIViewController+HUD.swift

import Foundation
import MBProgressHUD
// keyWindow
let KeyWindow : UIWindow = UIApplication.shared.keyWindow!

private var HUDKey = "HUDKey"
extension UIViewController
{
    var hud : MBProgressHUD?
        {
        get{
            return objc_getAssociatedObject(self, &HUDKey) as? MBProgressHUD
        }
        set{
            objc_setAssociatedObject(self, &HUDKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    /**
     显示提示信息(有菊花, 一直显示, 不消失),默认文字“加载中”,默认偏移量0

     - parameter view:    显示在哪个View上
     - parameter hint:    提示信息
     - parameter yOffset: y上的偏移量
     */
     func showHud(in view: UIView, hint: String = "加载中...", yOffset:CGFloat? = 0){
        let HUD = MBProgressHUD(view: view)
        HUD.label.text = hint
        HUD.label.font = UIFontSize(size: 15*UIRate)
        //设为false后点击屏幕其他地方有反应
        HUD.isUserInteractionEnabled = true
        //HUD内的内容的颜色
        HUD.contentColor = UIColor(red:0.82, green:0.82, blue:0.82, alpha:1.00)
        //View的颜色
        HUD.bezelView.color = UIColorHex("000000", 0.7)
        //style -blur 不透明 -solidColor 透明
        HUD.bezelView.style = .solidColor
        HUD.margin = 12*UIRate
        //偏移量,以center为起点
//        HUD.offset.y = yOffset ?? 0
        view.addSubview(HUD)
        HUD.show(animated: true)
        hud = HUD
    }

    /**
     显示纯文字提示信息(显示在keywindow上),默认时间2s,默认偏移量0

     - parameter hint: 提示信息
     - parameter duration: 持续时间(不填的话, 默认两秒)
     - parameter yOffset: y上的偏移量
     */
    func showHintInKeywindow(hint: String, duration: Double = 2.0, yOffset:CGFloat? = 0) {
        let view = KeyWindow
        let HUD = MBProgressHUD(view: view)
        view.addSubview(HUD)
        HUD.animationType = .zoomOut
        HUD.isUserInteractionEnabled = false
        HUD.bezelView.color = UIColor.black
        HUD.contentColor = UIColor.white
        HUD.mode = .text
        HUD.label.text = hint
        HUD.show(animated: true)
        HUD.removeFromSuperViewOnHide = false
        HUD.offset.y = yOffset ?? 0
        HUD.margin = 12*UIRate
        HUD.hide(animated: true, afterDelay: duration)
        hud = HUD
    }

    /**
     显示纯文字提示信息,默认时间1.5s,默认偏移量0

     - parameter view: 显示在哪个View上
     - parameter hint: 提示信息
     - parameter duration: 持续时间(不填的话, 默认两秒)
     - parameter yOffset: y上的偏移量
     */
    func showHint(in view: UIView, hint: String, duration: Double = 1.5, yOffset:CGFloat? = 0) {
        let HUD = MBProgressHUD(view: view)
        view.addSubview(HUD)
        HUD.animationType = .zoomOut
        HUD.bezelView.color = UIColor.black
        HUD.contentColor = UIColor.white
        HUD.mode = .text
        HUD.label.text = hint
        HUD.isUserInteractionEnabled = false
        HUD.removeFromSuperViewOnHide = false
        HUD.show(animated: true)
        HUD.offset.y = yOffset ?? 0
        HUD.margin = 12*UIRate
        HUD.hide(animated: true, afterDelay: duration)
        hud = HUD
    }

    /// 移除提示
    func hideHud() {
        //如果解包成功则移除,否则不做任何事
        if let hud = hud {
            hud.hide(animated: true)
        }
    }
   }

首先导入MBProgressHUD,然后进行封装,在使用时,在UIViewController 中

 //添加HUD
 self.showHud(in: self.view, hint: "加载中...")
//隐藏HUD
self.hideHud()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值