iOS-Developer-Swift

Swift基于MBProgress的扩展封装

2016-08-03  本文已影响790人  袁俊亮技术博客

Swift基于MBProgress的扩展封装
该封装是基于MBProgress的一些扩展需求,需要导入MBProgress框架以后才能使用该封装


import UIKit
import MBProgressHUD

let JLMBProgressMsgLoading : String = "正在加载..."
let JLMBProgressMsgError : String = "加载失败"
let JLMBProgressMsgSuccessful : String = "加载成功"
let JLMBProgressMsgNoMoreData : String = "没有更多数据了"
let JLMBProgressMsgTimeInterval : NSTimeInterval = 1.2

let font_size = CGFloat(15.0)
let opacity = CGFloat(0.85)


/// 提示类型
enum JLMBProgress {
    case Successful
    case Error
    case Warning
    case Info
}

class MBProgress_JLExtension{
    

}

//MARK: - 设置HUD的扩展
extension MBProgress_JLExtension {
    /// 添加到一个视图,并选择是否显示动画
    class func JL_ShowHUDAddedToView(view : UIView, title : String, animated : Bool) -> MBProgressHUD {
        let HUD = MBProgressHUD.showHUDAddedTo(view, animated: animated)
        HUD.label.font = UIFont.systemFontOfSize(font_size)
        HUD.label.text = title
        HUD.opacity = opacity
        return HUD
    }
    
    /// 有动画
    class func JL_ShowHUDAddToViewWithoutAnimate(view : UIView, title : String) -> MBProgressHUD {
        
        let HUD = MBProgressHUD.showHUDAddedTo(view, animated: true)
        HUD.label.font = UIFont.systemFontOfSize(font_size)
        HUD.label.text = title
        HUD.opacity = opacity
        return HUD
    }
    
    class func JL_ShowViewAfterSecond(title : String, view : UIView, afterSecond : NSTimeInterval) -> MBProgressHUD {
        
        let HUD = MBProgressHUD.showHUDAddedTo(view, animated: true)
        HUD.mode = MBProgressHUDMode.Text
        HUD.label.font = UIFont.systemFontOfSize(font_size)
        HUD.label.text = title
        HUD.opacity = opacity
        HUD.hideAnimated(true, afterDelay: afterSecond)
        return HUD
    }
    
    class func JL_ShowHUDHidAfterSecondWithMsgType(title : String, view : UIView, afterSecond : NSTimeInterval, msgType : JLMBProgress) -> MBProgressHUD {
        
        let HUD = MBProgressHUD.showHUDAddedTo(view, animated: true)
        HUD.label.font = UIFont.systemFontOfSize(font_size)
        let imageName = p_imageNamedWithMsgType(msgType)
        
        HUD.customView = UIImageView(image: UIImage(named: imageName))
        HUD.label.text = title
        HUD.opacity = opacity
        HUD.mode = MBProgressHUDMode.CustomView
        HUD.hideAnimated(true, afterDelay: afterSecond)
        return HUD
    }
    
    /// 根据显示类型来选择背景图片
    class func p_imageNamedWithMsgType(msgType : JLMBProgress) -> String {
        var imageName = ""
        
        switch msgType {
        case .Successful:
            imageName = "bwm_hud_success"
        case .Error:
            imageName = "bwm_hud_error"
        case .Warning:
            imageName = "bwm_hud_warning"
        case .Info:
            imageName = "bwm_hud_info"
        }
        return imageName
    }
}

上一篇下一篇

猜你喜欢

热点阅读