每次要加載一個頁面的時候都要等待幾秒,那麼在這幾秒之中很多人都喜歡放一個進度條或者小菊花之類的東西表示正在加載,今天呢,就寫一個關於進度條/小菊花的。
首先
在github上下載這個MBProgressHUD,地址https://github.com/jdg/MBProgressHUD
然後
MBProgressHUD.m 文件到你的工程裏面,(別忘了加上頭文件哦)
使用1:原始菊花類型
MBProgressHUD* hud = [[MBProgressHUD alloc]initWithView:self.view];
[self.view addSubview:hud];
//當前view背景顏色暗下去
hud.dimBackground =YES;
hud.labelText = @"haha";
[hud showAnimated:YES whileExecutingBlock:^{
sleep(2);
} completionBlock:^{
[hud removeFromSuperview];
}];
使用2:
MBProgressHUD* hud = [[MBProgressHUD alloc]initWithView:self.view];
[self.view addSubview:hud];
//當前view背景顏色暗下去
hud.dimBackground =YES;
hud.labelText = @"haha";
hud.mode = MBProgressHUDModeText;
[hud showAnimated:YES whileExecutingBlock:^{
float progress =0.0f;
while (progress<1.0f)
{
progress += 0.01f;
hud.progress =progress;
// 進程掛起一段時間, 單位是微秒(千分之一毫秒)
usleep(50000);
}
} completionBlock:^{
[hud removeFromSuperview];
}];
hud.mode的選擇,他有很多種
MBProgressHUDModeIndeterminate,
MBProgressHUDModeDeterminate,
MBProgressHUDModeDeterminateHorizontalBar,
MBProgressHUDModeAnnularDeterminate,
MBProgressHUDModeCustomView,
MBProgressHUDModeText
當然上面也可以同時放置文字和圖片,這就是另外一個屬性了
在hud.customView放一個imageView就可以了
OK,大功告成。