本文主要是介绍iOS MBProgressHUD的基本用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
详细教程:参考http://blog.csdn.net/jeffasd/article/details/51810501
详解:http://www.jianshu.com/p/485b8d75ccd4
typedef NS_ENUM(NSInteger, MBProgressHUDMode)
{// 使用UIActivityIndicatorView来显示进度,这是默认值
MBProgressHUDModeIndeterminate,
// 使用一个圆形饼图来作为进度视图
MBProgressHUDModeDeterminate,
// 使用一个水平进度条
MBProgressHUDModeDeterminateHorizontalBar,
// 使用圆环作为进度条
MBProgressHUDModeAnnularDeterminate,
// 显示一个自定义视图,通过这种方式,可以显示一个正确或错误的提示图
MBProgressHUDModeCustomView,
// 只显示文本
MBProgressHUDModeText
} MBProgressHUDMode;
//动画的效果
typedef NS_ENUM(NSInteger, MBProgressHUDAnimation) {
};
// 背景框的透明度,默认值是0.8
@property (assign) float opacity;
// 背景框的颜色
// 需要注意的是如果设置了这个属性,则opacity属性会失效,即不会有半透明效果
@property (MB_STRONG) UIColor *color;
// 背景框的圆角半径。默认值是10.0
@property (assign) float cornerRadius;
// 标题文本的字体及颜色
@property (MB_STRONG) UIFont* labelFont;
@property (MB_STRONG) UIColor* labelColor;
// 详情文本的字体及颜色
@property (MB_STRONG) UIFont* detailsLabelFont;
@property (MB_STRONG) UIColor* detailsLabelColor;
// 菊花的颜色,默认是白色
@property (MB_STRONG) UIColor *activityIndicatorColor;
属性是dimBackground,用于为HUD窗口的视图区域覆盖上一层径向渐变(radial gradient)层,其定义如下
@property (assign) BOOL dimBackground;
除了继承自UIView的-initWithFrame:初始化方法,MBProgressHUD还为我们提供了两个初始化方法,如下所示:
- (id)initWithWindow:(UIWindow *)window;
- (id)initWithView:(UIView *)view;
这两个方法分别传入一个UIWindow对象和一个UIView对象。传入的视图对象仅仅是做为MBProgressHUD视图定义其frame属性的参照,而不会直接将MBProgressHUD视图添加到传入的视图对象上。这个添加操作还得我们自行处理
// HUD相对于父视图中心点的x轴偏移量和y轴偏移量
@property (assign) float xOffset;
@property (assign) float yOffset;
// HUD各元素与HUD边缘的间距
@property (assign) float margin;
// HUD背景框的最小大小
@property (assign) CGSize minSize;
// HUD的实际大小
@property (atomic, assign, readonly) CGSize size;
// 是否强制HUD背景框宽高相等
@property (assign, getter = isSquare) BOOL square
MBProgressHUD提供了几种窗口模式,这几种模式的主要区别在于loading动画视图的展示。默认情况下,使用的是菊花(MBProgressHUDModeIndeterminate)。我们可以通过设置以下属性,来改变loading动画视图:
@property (assign) MBProgressHUDMode mode;
对于其它几种模式,MBProgressHUD专门我们提供了几个视图类。如果是进度条模式(MBProgressHUDModeDeterminateHorizontalBar),则使用的是MBBarProgressView类;如果是饼图模式(MBProgressHUDModeDeterminate)或环形模式(MBProgressHUDModeAnnularDeterminate),则使用的是MBRoundProgressView类。上面这两个类的主要操作就是在drawRect:中根据一些进度参数来绘制形状
我们还可以自定义loading动画视图,此时选择的模式是MBProgressHUDModeCustomView。或者不显示loading动画视图,而只显示文本框(MBProgressHUDModeText)。
// HUD显示和隐藏的动画类型
@property (assign) MBProgressHUDAnimation animationType;
// HUD显示的最短时间。设置这个值是为了避免HUD显示后立即被隐藏。默认值为0
@property (assign) float minShowTime;
// 这个属性设置了一个宽限期,它是在没有显示HUD窗口前被调用方法可能运行的时间。
// 如果被调用方法在宽限期内执行完,则HUD不会被显示。
// 这主要是为了避免在执行很短的任务时,去显示一个HUD窗口。
// 默认值是0。只有当任务状态是已知时,才支持宽限期。具体我们看实现代码。
@property (assign) float graceTime;
// 这是一个标识位,标明执行的操作正在处理中。这个属性是配合graceTime使用的。
// 如果没有设置graceTime,则这个标识是没有太大意义的。在使用showWhileExecuting:onTarget:withObject:animated:方法时,
// 会自动去设置这个属性为YES,其它情况下都需要我们自己手动设置。
@property (assign) BOOL taskInProgress;
// 隐藏时是否将HUD从父视图中移除,默认是NO。
@property (assign) BOOL removeFromSuperViewOnHide;
// 进度指示器,从0.0到1.0,默认值为0.0
@property (assign) float progress;
// 在HUD被隐藏后的回调
@property (copy) MBProgressHUDCompletionBlock completionBlock;
除了上面描述的实例方法之外,MBProgressHUD还为我们提供了几个便捷显示和隐藏HUD窗口的方法,如下所示:
=====+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated
+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated
=======
创建MBProgressHUD提示
-(void)MBProgressHUD{
1/// self指的是HUD要显示在那个view上
//方式1.直接在View上show
MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
//常用的设置
//小矩形的背景色
HUD.color = [UIColor clearColor];//这儿表示无背景
//显示的文字
HUD.labelText = @"发送消息成功";
//细节文字
HUD.detailsLabelText = @"socket消息";
//是否有庶罩
HUD.dimBackground = YES;
[HUD hide:YES afterDelay:5];
2//只显示文字
MBProgressHUD *hud = [MBProgressHUDshowHUDAddedTo:selfanimated:YES];
hud.mode =MBProgressHUDModeText;//显示的模式
hud.animationType=MBProgressHUDAnimationZoomOut;//带动画效果
hud.labelText =@"发送消息成功";
hud.labelColor=[UIColorredColor];
hud.margin =10.f;//文字和背景的距离
hud.yOffset =200.f;//整个MBView的Y的位置(起点位置是在view的中心附近)
hud.xOffset=10.0;//整个MBView的X的位置
hud.removeFromSuperViewOnHide =YES;
[hud hide:YESafterDelay:2];
///3/
//方式2.initWithView
MBProgressHUD* HUD = [[MBProgressHUD alloc] initWithView:self];
[self addSubview:HUD];
HUD.labelText = @"发送消息成功";
HUD.mode=MBProgressHUDModeAnnularDeterminate;//模式带小圆圈
[HUD showAnimated:YES whileExecutingBlock:^{
sleep(3);
hud.progress=progress;//进度值设置
} completionBlock:^{
}];
3// 水平进度条
MBProgressHUD* HUD = [[MBProgressHUD alloc] initWithView:self];
[self addSubview:HUD];
HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;//水平进度条
HUD.labelText = @"发送消息成功";
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
///4 // //自定义view
MBProgressHUD* HUD = [[MBProgressHUD alloc] initWithView:self];
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newfor"]];
[self addSubview:HUD];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"发送消息成功";
[HUD show:YES];
[HUD hide:YES afterDelay:3];
}
5.圆环进度条
-(void)circle{
MBProgressHUD * HUD = [[MBProgressHUDalloc]initWithView:self.view];
[self.viewaddSubview:HUD];
HUD.mode =MBProgressHUDModeAnnularDeterminate;
self.HUD=HUD;
HUD.labelText =@"Loading";
[HUD showWhileExecuting:@selector(myProgressTask)onTarget:selfwithObject:nilanimated:YES];
}
-(void)myProgressTask{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
self.HUD.progress = progress;
usleep(20000);
}
}
//-(void)myProgressTask{
// sleep(3);
//}
6.自定义的动画
-(void)autoAnimation{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode =MBProgressHUDModeCustomView;//显示的模式
hud.animationType=MBProgressHUDAnimationZoomOut;//带动画效果
UIImageView *animationImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 100)];
//自定义的单张图片
// animationImageView.image=[UIImage imageNamed:@"appstart"];
//**************imageView的动画
NSArray *magesArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"appstart"],
[UIImage imageNamed:@"chongzhi"],
[UIImage imageNamed:@"mycard"],nil];
animationImageView.animationImages = magesArray;//将序列帧数组赋给UIImageView的animationImages属性
animationImageView.animationDuration = 1;//设置动画时间
animationImageView.animationRepeatCount = 0;//设置动画次数 0表示无限
[animationImageView startAnimating];//开始播放动画
//**************imageView的动画 ***************
hud.customView=animationImageView;
hud.detailsLabelText=@"ceshi";
hud.labelText =@"发送消息成功";
hud.labelColor=[UIColor redColor];
hud.margin =10.f;//文字和背景的距离
hud.yOffset =200.f;//整个MBView的Y的位置(起点位置是在view的中心附近)
hud.xOffset=10.0;//整个MBView的X的位置
hud.removeFromSuperViewOnHide =YES;
[hud hide:YES afterDelay:15];
}
- (void)updateIndicators
方法在
if (mode == MBProgressHUDModeIndeterminate) {
if (!isActivityIndicator) {
// Update to indeterminate indicator
[indicator removeFromSuperview];
self.indicator = MB_AUTORELEASE([[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]);
self.indicator = MB_AUTORELEASE([[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 40)]);
self.indicator.contentMode = UIViewContentModeScaleAspectFit;
// [(UIActivityIndicatorView *)indicator startAnimating];
[self addSubview:indicator];
for (int i=0; i<24; i++) {
[_loadingArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"loading_%d",i + 1]]];
}
//设置动画数组
[(UIImageView *)self.indicator setAnimationImages:_loadingArray];
//设置动画播放次数
[(UIImageView *)self.indicator setAnimationRepeatCount:MAXFLOAT];
//设置动画播放时间
[(UIImageView *)self.indicator setAnimationDuration:1.5];
//开始动画
[(UIImageView *)self.indicator startAnimating];
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000
if ([indicator isKindOfClass:[UIActivityIndicatorView class]]) {
[(UIActivityIndicatorView *)indicator setColor:self.activityIndicatorColor];
}
#endif
}
7.
//无动画自定义View(view代替了hud.text和hud.detailtext,也可以同时显示自定义View和hud.text和hud.detailtext(只要设置hud.text和hud.detailtext就可以了),)
-(void)showProgresshudTextWith:(UIView *)view title:(NSString *)title detailtitle:(NSString *)detailTitle isOnDismissbg:(BOOL)ison hideAfterdelay:(BOOL)hideAfterdelay isCustomView:(BOOL)isCustomView{
if(view){
MBProgressHUD *hud=[[MBProgressHUDalloc]initWithView:view];
[view addSubview:hud];
hud.mode= MBProgressHUDModeCustomView;
UILabel *lbl=[[UILabelalloc]initWithFrame:CGRectMake(0,0, 200, 100)];
lbl.font=[UIFontsystemFontOfSize:12];
lbl.numberOfLines=0;
hud.customView=lbl;
hud.color=[UIColorcolorWithWhite:0.8alpha:0.8];
hud.minSize=CGSizeMake(200,100);
hud.animationType=MBProgressHUDAnimationZoomIn;
if(title && ![title isEqualToString:@""]){
lbl.text=title;
}
if(ison){
hud.dimBackground=YES;
}
[hud show:YES];
if(hideAfterdelay){
[hud hide:YESafterDelay:2];
}
}
}
==================
UIWindow *window = [UIApplicationsharedApplication].keyWindow;
[MBProgressHUDhideAllHUDsForView:windowanimated:YES];//清除所有的hud
_hud = [MBProgressHUD showHUDAddedTo:window animated:YES]
==================
/**
* 在某个view上添加HUD并显示
*
* 注意:显示之前,先去掉在当前view上显示的HUD。这个做法很严谨,我们将这个方案抽象出来:如果一个模型是这样的:我们需要将A加入到B中,但是需求上B里面只允许只有一个A。那么每次将A添加到B之前,都要先判断当前的b里面是否有A,如果有,则移除。
*/
//根据要给哪个view添加hud,实例化hud
+ (instancetype)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
/**
* 找到某个view上最上层的HUD并隐藏它。
* 如果返回值是YES的话,就表明HUD被找到而且被移除了。
*/
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated;
/**
* 在某个view上找到最上层的HUD并返回它。
* 返回值可以是空,所以返回值的关键字为:nullable
*/
+ (nullableMBProgressHUD *)HUDForView:(UIView *)view;
1.3 对象方法:
/**
* 一个HUD的便利构造函数,用某个view来初始化HUD:这个view的bounds就是HUD的bounds
*/
- (instancetype)initWithView:(UIView *)view;
/**
* 显示HUD,有无动画。
*/
- (void)showAnimated:(BOOL)animated;
/**
* 隐藏HUD,有无动画。
*/
- (void)hideAnimated:(BOOL)animated;
/**
* 在delay的时间过后隐藏HUD,有无动画。
*/
- (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)delay;
这篇关于iOS MBProgressHUD的基本用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!