MBProgressHUD作為 github上一款頗受大家歡迎的第三方提示框框架,為很多iOS開發(fā)者喜愛俐东。
MBProgressHUD
官網(wǎng)地址:https://github.com/jdg/MBProgressHUD
MBProgressHUD使用時跌穗,最好再封裝一下,繼承自MBProgressHUD虏辫,它提供了各種(mode)樣式蚌吸,以下是官方的枚舉的各種樣式
typedefNS_ENUM(NSInteger, MBProgressHUDMode) {
/**?Progress?is?shown?using?an?UIActivityIndicatorView.?This?is?the?default.?*/
MBProgressHUDModeIndeterminate,//默認的小菊花轉(zhuǎn)動的樣式
/**?Progress?is?shown?using?a?round,?pie-chart?like,?progress?view.?*/
MBProgressHUDModeDeterminate,//用一個圓,餅狀圖的樣式
/**?Progress?is?shown?using?a?horizontal?progress?bar?*/
MBProgressHUDModeDeterminateHorizontalBar,//水平進度條
/**?Progress?is?shown?using?a?ring-shaped?progress?view.?*/
MBProgressHUDModeAnnularDeterminate,//圓環(huán)狀的進度條
/**?Shows?a?custom?view?*/
MBProgressHUDModeCustomView,//自定義視圖砌庄,可以添加自定義圖片
/**?Shows?only?labels?*/
MBProgressHUDModeText//只顯示標簽提示羹唠,如“加載中......”
};
可以新建一個文件,使用類方法娄昆,例如:
+(id)showWithIndicator:(NSString*)text;//傳nil用默認的
+(id)showwithTextDailog:(NSString*)string;//小菊花
+(id)showwithCircleDailog:(NSString*)string;//餅狀圖
+(void)hide;//隱藏hud
實現(xiàn)這個方法
+(id)showwithCircleDailog:(NSString*)string
{
@synchronized(HUD){
UIWindow*window?=?[[[UIApplicationsharedApplication]windows]objectAtIndex:0];
if(!HUD)?{
HUD=?[[HUDViewalloc]initWithView:window];
[windowaddSubview:HUD];
}
HUD.opacity=0.7f;//更改視圖的背景透明圖佩微,第三方是0.8的透明度,可以根據(jù)自己的需要修改(備注萌焰,在結(jié)尾)
HUD.mode=MBProgressHUDModeAnnularDeterminate;// 樣式哺眯,可以根據(jù)需要修改
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
// Do something useful in the background and update the HUD periodically.
[HUD doSomeWorkWithProgress];//調(diào)用加載效果,比如進度條慢慢填滿
});
HUD.labelText=?string;//提示框的文字
[HUD show:YES];//提示框出現(xiàn)
[HUD hide:YES afterDelay:3];//提示框出現(xiàn)持續(xù)的時間長短,根據(jù)自己的需要設置長短
return HUD;
}
}
//自定義圖片
+(id)showwithCustomRightDailog:(NSString *)string;
{
@synchronized(HUD){
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
if (!HUD) {
HUD = [[HUDView alloc] initWithView:window];
[window addSubview:HUD];
}
UIImage *image = [[UIImage imageNamed:@"Checkmarkfabu"] imageWithRenderingMode:UIImageRenderingModeAutomatic];//自定義圖片
HUD.customView = [[UIImageView alloc] initWithImage:image];
HUD.opacity = 0.7f;
HUD.mode =? MBProgressHUDModeCustomView;
HUD.labelText = string;
[HUD show:YES];
[HUD hide:YES afterDelay:1.3];
return HUD;
}
}
//調(diào)用加載效果扒俯,參考官方demos
- (void)doSomeWorkWithProgress {
self.canceled = NO;
// This just increases the progress indicator in a loop.
float progress = 0.0f;//初始化進度是0
while (progress < 1.0f) {
if (self.canceled) break;
progress += 0.04f;//進度加載速度
dispatch_async(dispatch_get_main_queue(), ^{
// Instead we could have also passed a reference to the HUD
// to the HUD to myProgressTask as a method parameter.
HUD.progress = progress;
});
usleep(50000);
}
}
//隱藏hud
+(void)hide
{
if (HUD) {
[HUD hide:YES];
HUD = Nil;
}
}
具體使用的時候奶卓,可以將上面的文件引入pch文件,這樣全局都可以使用撼玄。使用的時候夺姑,直接在代碼中敲中括號,在中括號內(nèi)寫上[Hudview showwithCircleDailog:@"加載完成"]掌猛,這樣就能顯示你想要的輸入框了盏浙。
部分效果:
備注:
MBProgressHUD的提示框的背景圖的透明度修改,不要直接在MBProgressHUD的源文件里面修改,這樣提交svn或者更新pods的時候废膘,會出現(xiàn)很多的問題竹海,以下是源文件修改的地方。最好在自己創(chuàng)建的繼承自MBProgressHUD的文件里面修改殖卑,這樣更方便站削,還能盡可能少出問題。MBProgressHUD還有其他的一些屬性孵稽,也不要直接在源文件里面修改许起。