之前給大家分享過系統(tǒng)的中間彈框,底部彈框UIAlertController,今天來補(bǔ)充一下MBProgressHUD的應(yīng)用吧 YoY
首先我用Cocoapods導(dǎo)入了MBProgressHUD這個(gè)第三方庫,關(guān)于Cocoapods的安裝與使用就不給大家詳細(xì)講解啦,有一篇很好的文章分享給大家:CocoaPods的安裝使用和常見問題http://www.lxweimin.com/p/6e5c0f78200a
好啦好啦,跑遠(yuǎn)啦,嘿嘿,導(dǎo)入MBProgressHUD之后要記得創(chuàng)建了橋接文件哦,我們現(xiàn)在寫的可是swift呀。創(chuàng)建文件以后千萬要記得設(shè)置這里:
設(shè)置橋接文件.png
好啦,一切準(zhǔn)備就緒,開始敲代碼:
首先,我們在storyboard里面創(chuàng)建四個(gè)按鈕:
首頁.png
我們最常見的提示框
@IBAction func defaultMBProgress(sender: AnyObject) {
let HUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
HUD.delegate = self
//常用設(shè)置
//小矩形的背景色
HUD.bezelView.color = UIColor.clearColor()
//顯示的文字
HUD.label.text = "加載中1..."
//細(xì)節(jié)文字
HUD.detailsLabel.text = "請耐心等待..."
//設(shè)置背景,加遮罩
HUD.backgroundView.style = .Blur //或SolidColor
HUD.hideAnimated(true, afterDelay: 2)
}
我們最常見的提示框.png
只有文字的提示框
@IBAction func textMBProgress(sender: UIButton) {
//只顯示文字
let hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hud.mode = MBProgressHUDMode.Text
hud.label.text = "啦啦啦..."
hud.detailsLabel.text = "這是詳細(xì)信息"
hud.margin = 10
hud.offset.y = 50
hud.removeFromSuperViewOnHide = true
hud.hideAnimated(true, afterDelay: 3)
}
只有文字的提示框.png
自定義的提示框
@IBAction func customMBProgress(sender: UIButton) {
//自定義視圖提示
let hud1 = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hud1.mode = MBProgressHUDMode.CustomView
hud1.customView = UIImageView(image: UIImage(named: "pic_dui@2x"))
hud1.label.text = "自定義加載..."
hud1.offset.y = -50
hud1.hideAnimated(true, afterDelay: 4)
}
自定義的提示框.png
使用異步加載的提示框
前面的幾種提示框都很好理解,在多數(shù)項(xiàng)目中,還會(huì)涉及到異步操作的等待提示,比如網(wǎng)絡(luò)下載數(shù)據(jù),那就要在提示的時(shí)候,后臺(tái)下載數(shù)據(jù),完成后再自動(dòng)隱藏掉。
@IBAction func asyncShow(sender: UIButton) {
let hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hud.label.text = "請稍等,數(shù)據(jù)加載中,預(yù)計(jì)10秒中"
hud.showAnimated(true, whileExecutingBlock: {
//異步任務(wù),在后臺(tái)運(yùn)行的任務(wù)
sleep(10)
}) {
//執(zhí)行完成后的操作,移除
hud.removeFromSuperview()
}
}
這個(gè)方法:
hud.showAnimated(<#T##animated: Bool##Bool#>, whileExecutingBlock: <#T##dispatch_block_t##dispatch_block_t##() -> Void#>)
現(xiàn)在已經(jīng)廢棄了,但是也可以用,我們可以按照它的原理用GCD來實(shí)現(xiàn)。
使用異步加載的提示框.gif