二、多功能提示框——MBProgressHUD


概述

(注:圖片僅展示部分功能,圖片來自github)

MBProgressHUD是一個Objective-C開源庫,它可以讓你在UI界面界面上插入一個透明的方框,伴以文字或進圖條等,從而提示一些后台信息。

 

安裝

一如既往,首先創建一個工程,此處命名為Charpter2MBProgressHUD。

 

關閉XCode,使用終端進入到工程所在目錄(Charpter2MBProgressHUD),並運行"pod init"。

 

根據github上的指引,修改Podfile,添加一行“pod 'MBProgressHUD', '~> 1.1.0'”。

 

 

接着運行“pod install”安裝MBProgressHUD。

 

在工程目錄Charpter2MBProgressHUD下雙擊如下圖所示白色工程文件,然后XCode會重新打開。

 

由於MBProgressHUD是Objective-C寫成的庫,所以Swift要使用它還必須添加橋接文件。

此處我們新建一個橋接文件,並命名為“BridgeHeader.h”,並在該文件中添加:

#import "MBProgressHUD.h"

 

接下來將MBProgressHUD的接口文件拖動到工程里(如下圖箭頭方向)。

 

最后,我們在XCode的編譯選項中指定該文件為橋接文件(按下圖箭頭方向拖動BridgeHeader.h)

 

OK,這樣MBProgressHUD庫就算安裝完成啦,現在我們來試試效果。

 

小試牛刀 

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8     }
 9 
10     override func didReceiveMemoryWarning() {
11         super.didReceiveMemoryWarning()
12         // Dispose of any resources that can be recreated.
13     }
14 
15     @IBAction func actionLoading(_ sender: UIButton) {
16         let HUD = MBProgressHUD.showAdded(to: self.view, animated: true)
17         HUD.label.text = "加載中..."
18         HUD.hide(animated: true, afterDelay: 3)
19     }
20     @IBAction func actionNote(_ sender: UIButton) {
21         let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
22         hud.mode = MBProgressHUDMode.text
23         hud.label.text = "標題"
24         hud.detailsLabel.text = "詳情"
25         hud.hide(animated: true, afterDelay: 3)
26     }
27     
28 }

 

添加2個按鈕,分別對應觸發兩種效果的MBProgressHUD。

 

上一節           回目錄          


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM