天天看點

Swift項目中使用MBProgressHUD

Swift項目引入MBProgressHUD庫

MBProgressHUD的源碼很簡單,就一個h接口檔案和一個m實作檔案,一共2個檔案。

下載下傳後,把兩個檔案拖到Swift項目中,XCode會提示是否建立橋接檔案,選擇是後,會自動建立一個Bridging-Header.h檔案。

Swift項目中使用MBProgressHUD

我們需要在這個橋接檔案中,引入MBProgressHUD庫,輸入:

Swift項目中使用MBProgressHUD

導入工作就完成了,現在可以調用MBProgressHUD的相關方法使用了。

在Swift中調用MBProgressHUD

代碼很簡單,羅列了三種類型,分别是預設的菊花提示、純文字提示、自定義視圖提示。

預設提示:

func defaultShow(){
    var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true) hud.labelText = "這是預設帶菊花的提示" //背景漸變效果 hud.dimBackground = true //延遲隐藏 hud.hide(true, afterDelay: 0.8) }
           

效果圖:

Swift項目中使用MBProgressHUD

純文字提示:

func textShow(){
    var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true) hud.mode = MBProgressHUDMode.Text hud.labelText = "這是純文字提示" hud.detailsLabelText = "這是詳細資訊内容,會很長很長呢" //延遲隐藏 hud.hide(true, afterDelay: 0.8) }
           

效果圖:

Swift項目中使用MBProgressHUD

自定義視圖提示:

func customShow(){
    
    var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true) hud.mode = MBProgressHUDMode.CustomView hud.customView = UIImageView(image: UIImage(named: "yes")!) hud.labelText = "這是自定義視圖" //延遲隐藏 hud.hide(true, afterDelay: 0.8) }
           

效果圖:

Swift項目中使用MBProgressHUD

使用異步功能

前面我們隻實作了一個單純的提示框功能,它會自動隐藏掉。在多數項目中,還會涉及到異步操作的等待提示,比如網絡下載下傳資料,那就要在提示的時候,背景下載下傳資料,完成後再自動隐藏掉。

func asyncShow(){
    var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true) hud.labelText = "請稍等,資料加載中,預計10秒中" hud.showAnimated(true, whileExecutingBlock: { //異步任務,在背景運作的任務 sleep(10) }) { //執行完成後的操作,移除 hud.removeFromSuperview() hud = nil } }
           

轉載于:https://www.cnblogs.com/mkai/p/6495732.html