本文使用swift语言使用MBProgressHUD。
开源项目MBProgressHUD可以实现多种形式的提示框。使用简单,方便。
GitHud的下载地址是:https://github.com/jdg/MBProgressHUD/
下载完成后,将MBProgressHUD.h和MBProgressHUD.m拖入已经新建好的Swift项目。因为使用的swift语言,所以拖入项目的时候会提示是否新建一个桥接objective-c与swift的文件,选择是即可。此步骤会自动新建一个文件。如图:
在该文件(MBProgressHUDDemo-Bridging-Header.h)中,有这样的一句注释:
// Use this file to import your target’s public headers that you would like to expose to Swift.
意思应该是导入头文件,使得你的swift可以使用拖进去的文件。
因此,在该文件中写这么一句:
#import "MBProgressHUD.h"
会发现在Build Settings–>Swift complier -Code Generation中,多了一个项,个人理解是桥接文件MBProgressHUDDemo-Bridging-Header.h的声明。如果有不同见解,欢迎指正。如图:
尝试了几种样式的提示框。使用按键来触发。
以下是整个Demo的代码:
import UIKit
class ViewController: UIViewController {
@IBAction func TextDialogBtn(sender: AnyObject) {
showTextDialog()
}
@IBAction func ProgressDialogBtn1(sender: AnyObject) {
showProgressDialog1()
}
@IBAction func ProgressDialogBtn2(sender: AnyObject) {
showProgressDialog2()
}
@IBAction func CustomDialogBtn(sender: AnyObject) {
showCustomDialog()
}
@IBAction func AllTextDialogBtn(sender: AnyObject) {
showAllTextDialog()
}
var HUD : MBProgressHUD?
override func viewDidLoad() {
super.viewDidLoad()
}
func showTextDialog(){
HUD = MBProgressHUD(view: self.view)
self.view.addSubview(HUD!)
HUD?.dimBackground = true
HUD?.labelText = "请稍等"
HUD?.showAnimated(true, whileExecutingBlock: {
sleep(3)
}, completionBlock: {
self.HUD?.removeFromSuperview()
self.HUD = nil
})
}
func showProgressDialog1(){
HUD = MBProgressHUD(view: self.view)
self.view.addSubview(HUD!)
HUD?.dimBackground = true
HUD?.labelText = "正在加载"
HUD?.mode = MBProgressHUDMode.Determinate
HUD?.showAnimated(true, whileExecutingBlock: {
var progress : Float = 0.0
while(progress < 1.0){
progress += 0.01
self.HUD?.progress = progress
usleep(50000)
}
}, completionBlock: {
self.HUD?.removeFromSuperview()
self.HUD = nil
})
}
func showProgressDialog2(){
HUD = MBProgressHUD(view: self.view)
self.view.addSubview(HUD!)
HUD?.dimBackground = true
HUD?.labelText = "正在加载"
HUD?.mode = MBProgressHUDMode.DeterminateHorizontalBar
HUD?.showAnimated(true, whileExecutingBlock: {
var progress : Float = 0.0
while(progress < 1.0){
progress += 0.01
self.HUD?.progress = progress
usleep(50000)
}
}, completionBlock: {
self.HUD?.removeFromSuperview()
self.HUD = nil
})
}
func showCustomDialog(){
HUD = MBProgressHUD(view: self.view)
self.view.addSubview(HUD!)
HUD?.dimBackground = true
HUD?.labelText = "操作成功"
HUD?.mode = MBProgressHUDMode.CustomView
HUD?.customView = UIImageView(image: UIImage(named: "37x-Checkmark-1"))
HUD?.showAnimated(true, whileExecutingBlock: {
sleep(2)
}, completionBlock: {
self.HUD?.removeFromSuperview()
self.HUD = nil
})
}
func showAllTextDialog(){
HUD = MBProgressHUD(view: self.view)
self.view.addSubview(HUD!)
HUD?.dimBackground = true
HUD?.mode = MBProgressHUDMode.Text
HUD?.labelText = "操作成功"
HUD?.showAnimated(true, whileExecutingBlock: {
sleep(2)
}, completionBlock: {
self.HUD?.removeFromSuperview()
self.HUD = nil
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
以下是运行的效果:
文本提示框:
进度提示框1:
进度提示框2:
自定义提示框:
纯文本提示框: