美文网首页iOS开发ios
iOS--MBProgressHUD提示框

iOS--MBProgressHUD提示框

作者: 郭荣伟 | 来源:发表于2016-03-15 18:17 被阅读8833次

MBProgressHUD作为 github上一款颇受大家欢迎的第三方提示框框架,为很多iOS开发者喜爱。

MBProgressHUD
官网地址:https://github.com/jdg/MBProgressHUD

MBProgressHUD使用时,最好再封装一下,继承自MBProgressHUD,它提供了各种(mode)样式,以下是官方的枚举的各种样式

typedefNS_ENUM(NSInteger, MBProgressHUDMode) {

/** Progress is shown using an UIActivityIndicatorView. This is the default. */

MBProgressHUDModeIndeterminate,//默认的小菊花转动的样式

/** 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,//圆环状的进度条

/** 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

实现这个方法

+(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的透明度,可以根据自己的需要修改(备注,在结尾

HUD.mode=MBProgressHUDModeAnnularDeterminate;// 样式,可以根据需要修改

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{

// Do something useful in the background and update the HUD periodically.

[HUD doSomeWorkWithProgress];//调用加载效果,比如进度条慢慢填满

});

HUD.labelText= string;//提示框的文字

[HUD show:YES];//提示框出现

[HUD hide:YES afterDelay:3];//提示框出现持续的时间长短,根据自己的需要设置长短

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;

}

}

//调用加载效果,参考官方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文件,这样全局都可以使用。使用的时候,直接在代码中敲中括号,在中括号内写上[Hudview showwithCircleDailog:@"加载完成"],这样就能显示你想要的输入框了。

部分效果:

备注:

MBProgressHUD的提示框的背景图的透明度修改,不要直接在MBProgressHUD的源文件里面修改,这样提交svn或者更新pods的时候,会出现很多的问题,以下是源文件修改的地方。最好在自己创建的继承自MBProgressHUD的文件里面修改,这样更方便,还能尽可能少出问题。MBProgressHUD还有其他的一些属性,也不要直接在源文件里面修改。

相关文章

网友评论

本文标题:iOS--MBProgressHUD提示框

本文链接:https://www.haomeiwen.com/subject/rabflttx.html