您的位置:首页 > 移动开发 > IOS开发

MBProgressHUD(透明指示层)详细使用

2016-05-17 20:51 429 查看

MBProgressHUD(透明指示层)详细使用

MBProgressHUD的使用场景:MBProgressHUD主要用于执行任务时的过程指示,给用户信息以明确当前正在执行的操作,解答用户的疑惑,缓解用户的等待焦急症。例如登陆过程的等待,加载数据的等待等。

MBProgressHUD的使用主要分为两种情况。其一:通过类方法直接实例化对象,这种方式不需要用户手动开始,也不需要用户手动添加MBProgressHUD视图到其父视图上。这种放肆简单,但灵活度低。其二:与第一种方法相对应,手动开始显示,同时可以指定期间执行的任务,以及显示完成执行的任务。

1. 类方法实例化

// 显示HUD
MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

// 在这里执行相应的任务

// 执行任务完成,隐藏
BOOL isHide = [MBProgressHUD hideHUDForView:self.view animated:YES];


这种方式的实例化,会自动的将HUD添加到传入的视图中,不用用户手动

2. 对象方法实例化

// 实例化HUD
MBProgressHUD * hud = [[MBProgressHUD alloc] initWithView:self.view];
// 添加到实例化中传入的视图,这里注意两个view一定要一致
[self.view addSubview:hud];

// 这里主要有两种显示方法:
// 第一种:通过Block
[hud showAnimated:YES whileExecutingBlock:^{
// 显示期间执行的Block
} onQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionBlock:^{
// HUD隐藏后执行的Block
}];

// 第二种:通过selector的方式
[hud showWhileExecuting:@selector(update) onTarget:self withObject:nil animated:YES];

// 但隐藏需要手动隐藏:任然调用下面的方法
BOOL isHide = [MBProgressHUD hideHUDForView:self.view animated:YES];


当添加多个HUD的时候,MBProgressHUD提供隐藏的快捷方法:

[MBProgressHUD hideAllHUDsForView:self.view animated:YES];


当然MBProgressHUD也提供了根据视图获取对应的HUD的方法:

NSArray * huds = [MBProgressHUD HUDForView:self.view];


3. 属性详解

MBProgressHUD提供了十分多的属性,这是我们给视图天使HUD更加的灵活多变,下面对其中常用的属性进行解释:

// 设置显示的HUD模式
hud.mode = MBProgressHUDModeDeterminate;
/*
MBProgressHUDModeIndeterminate,              // default模式,显示活动指示器
MBProgressHUDModeDeterminate,                // 扇形图显示进度
MBProgressHUDModeDeterminateHorizontalBar,   // 按钮形状式显示进度
MBProgressHUDModeAnnularDeterminate,         // 圆环式显示进度
MBProgressHUDModeCustomView,                 // 自定义显示视图
MBProgressHUDModeText                        // 只显示主副标题
*/
// 注意以上显示模式除了最后一种,其余的都可以显示主副标题(条件是设置了labelText、detailsLabelText属性)

// 设置HUD背景颜色
hud.color = [UIColor whiteColor];
// 主标题
hud.labelText = @"牛逼的梦想";
// 副标题
hud.detailsLabelText = @"我是很牛逼的梦想";
// 显示、隐藏动画样式
hud.animationType = MBProgressHUDAnimationZoomIn;
/*
MBProgressHUDAnimationFade,    // 淡入、淡出模式
MBProgressHUDAnimationZoomOut  // 淡入、淡出 + 缩放
MBProgressHUDAnimationZoomIn   // 淡入、淡出 + 缩放
*/

// 当model属性为显示进度相关的值时,此时设置progress的值,可以实现进度的显示
hud.progress = 1.0;

// HUD的相对于父视图 x 的偏移,默认居中
hud.xOffset = 50;
hud.yOffset = 50;
// 是否显示蒙板
hud.dimBackground = YES;
// HUD内部视图相对于HUD的边距
hud.margin = 50;
// HUD圆角半径
hud.cornerRadius = 20;
// 最小的显示时间
hud.minShowTime = 3;
// HUD的最小尺寸
hud.minSize = CGSizeMake(300, 300);
// 代理中只有一个方法,即获得HUD隐藏后的时刻
hud.delegate = self;

// 另外还有一些与labelText、detailsLabelText相关的属性,这里不介绍了。另外还有一个customView属性,将会在下面进行讲解


4. 自定义显示视图

MBProgressHUD不光提供了普通HUD的简单实用,还提供了自定义显示内容的简单接口,下面的例子只是简单的实现系统提供的活动指示器样式

// 实例化HUD
MBProgressHUD * hud = [[MBProgressHUD alloc] initWithView:self.view];
// 添加到实例化中传入的视图,这里注意两个view一定要一致
[self.view addSubview:hud];

// 自定义视图
UIActivityIndicato
4000
rView * indictorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indictorView startAnimating];
hud.customView = indictorView;

// 显示HUD
[hud showWhileExecuting:@selector(update) onTarget:self withObject:nil animated:YES];

// 隐藏HUD
[MBProgressHUD hideHUDForView:self.view animated:YES];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS MBProgress HUD