Github、cocoachina、cocoa4app等尋找了很多swift版本的HUD,都不太滿意。綜合對比,並沒有找到好用合適的HUD開源庫。於是乎 SwiftProgressHUD
是目前比較好用美觀、多功能的HUD。用法如下:
要求
- swift 3.0+
- iOS 8.0+
- ARC
添加 SwiftProgressHUD
到你的工程
一、CocoaPods
1.將 SwiftProgressHUD
添加到你的 Podfile
pod 'SwiftProgressHUD'
2.安裝或者更新
pod install
// 或者
pod update
3.在你需要使用 SwiftProgressHUD
的地方import
import SwiftProgressHUD
二、Carthage
1.將 SwiftProgressHUD
添加到你的 Cartfile
github "YJManager/SwiftProgressHUD"
2.編譯安裝
carthage update
3.配置
1.點擊工程 -> 2.Target -> 3.Linked Frameworks and Libaries ->
4.點擊+號 ->Add Other... -> 5.找到Framework選中點擊open ->
6. build phases -> 7.New Run Script Phase -> 8.將 /usr/local/bin/carthage copy-frameworks 拷貝到Script ->
9. Input Files —> $(SRCROOT)/Carthage/Build/iOS/[你的框架名稱]
使用舉例
在需要等待的時候, 可以使用 showWait
, 代碼及效果如下:
/// 設置蒙版背景顏色, 默認是clear
// SwiftProgressHUD.hudBackgroundColor = UIColor.black.withAlphaComponent(0.2)
/// 開始loading...
SwiftProgressHUD.showWait()
/// 模擬 1s後 加載完成
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {SwiftProgressHUD.hideAllHUD()}
加載成功時, 提示可以使用 showSuccess
, 代碼及效果如下:
SwiftProgressHUD.showSuccess("加載成功")
/// 模擬 1s後 加載完成
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {SwiftProgressHUD.hideAllHUD()}
加載失敗時, 提示可以使用 showFail
, 代碼及效果如下:
SwiftProgressHUD.showFail("加載失敗")
/// 模擬 1s後 加載完成
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {SwiftProgressHUD.hideAllHUD()}
友情提示信息, 可以使用 showInfo
, 代碼及效果如下:
SwiftProgressHUD.showInfo("請稍後")
/// 模擬 1s後 加載完成
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {SwiftProgressHUD.hideAllHUD()}
錯誤Toast提示信息, 可以使用 showOnlyText
, 代碼及效果如下:
SwiftProgressHUD.showOnlyText("請輸入合法的手機號")
/// 模擬 1s後 加載完成
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {SwiftProgressHUD.hideAllHUD()}
狀態欄提示信息, 可以使用 showOnStatusBar
, 代碼及效果如下:
SwiftProgressHUD.showOnStatusBar("你有一條新消息", autoClear: true, autoClearTime: 1, textColor: UIColor.orange, backgroundColor: UIColor.lightGray)
/// 模擬 1s後 加載完成
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {SwiftProgressHUD.hideAllHUD()}
加載動畫提示, 可以使用 showAnimationImages
, 代碼及效果如下:
let animationDuration = 70 // 動畫時間 單位毫秒Int
var loadingImages = [UIImage]()
for index in 0...16 {
let loadImageName = String(format: "new_brand_progress%02d", index)
if let loadImage = UIImage(named: loadImageName){
loadingImages.append(loadImage)
}
}
SwiftProgressHUD.showAnimationImages(loadingImages, timeMilliseconds: animationDuration, backgroundColor: backgroundColor, scale: scale)
/// 模擬 1s後 加載完成
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {SwiftProgressHUD.hideAllHUD()}
詳細的用法請參考 SwiftProgressHUDDemo