在码代码过程中,我们经常用到MBProgressHUD,但我很少实例化使用,一般都是偷个懒直接显示隐藏,这里贴上详解,以便日后有样式要求时使用。
1,MBProgressHUD常用属性和用法Demo
- (void)testMBProgressHUD{ NSLog(@"test MBProgressHUD "); /* 要将一个MBProgressHUD显示出来,1,创建对象;2,将HUD添加到view上;3,调用show方法 隐藏,1,hide:方法; 2,hide: afterDelay: 方法 其它的用法都是特殊的设置等 */ HUD = [[MBProgressHUD alloc] init]; [self.view addSubview:HUD];// HUD.mode = MBProgressHUDModeIndeterminate;//菊花,默认值// HUD.mode = MBProgressHUDModeDeterminate;//圆饼,饼状图// HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;//进度条 HUD.mode = MBProgressHUDModeAnnularDeterminate;//圆环作为进度条// HUD.mode = MBProgressHUDModeCustomView; //需要设置自定义视图时候设置成这个// HUD.mode = MBProgressHUDModeText; //只显示文本//1,设置背景框的透明度 默认0.8 HUD.opacity =1;//2,设置背景框的背景颜色和透明度, 设置背景颜色之后opacity属性的设置将会失效 HUD.color = [UIColor redColor]; HUD.color = [HUD.color colorWithAlphaComponent:1];//3,设置背景框的圆角值,默认是10 HUD.cornerRadius =20.0;//4,设置提示信息 信息颜色,字体 HUD.labelColor = [UIColor blueColor]; HUD.labelFont = [UIFont systemFontOfSize:13]; HUD.labelText = @"Loading...";//5,设置提示信息详情 详情颜色,字体 HUD.detailsLabelColor = [UIColor blueColor]; HUD.detailsLabelFont = [UIFont systemFontOfSize:13]; HUD.detailsLabelText = @"LoadingLoading...";//6,设置菊花颜色 只能设置菊花的颜色 HUD.activityIndicatorColor = [UIColor blackColor];//7,设置一个渐变层 HUD.dimBackground = YES;//8,设置动画的模式// HUD.mode = MBProgressHUDModeIndeterminate;//9,设置提示框的相对于父视图中心点的便宜,正值 向右下偏移,负值左上 HUD.xOffset = -80; HUD.yOffset = -100;//10,设置各个元素距离矩形边框的距离 HUD.margin =0;//11,背景框的最小大小 HUD.minSize = CGSizeMake(50,50);//12设置背景框的实际大小 readonly CGSize size = HUD.size;//13,是否强制背景框宽高相等 HUD.square = YES;//14,设置显示和隐藏动画类型 有三种动画效果,如下// HUD.animationType = MBProgressHUDAnimationFade; //默认类型的,渐变// HUD.animationType = MBProgressHUDAnimationZoomOut; //HUD的整个view后退 然后逐渐的后退 HUD.animationType = MBProgressHUDAnimationZoomIn;//和上一个相反,前近,最后淡化消失//15,设置最短显示时间,为了避免显示后立刻被隐藏 默认是0// HUD.minShowTime = 10;//16, /*// 这个属性设置了一个宽限期,它是在没有显示HUD窗口前被调用方法可能运行的时间。// 如果被调用方法在宽限期内执行完,则HUD不会被显示。// 这主要是为了避免在执行很短的任务时,去显示一个HUD窗口。// 默认值是0。只有当任务状态是已知时,才支持宽限期。具体我们看实现代码。 @property (assign)float graceTime;// 这是一个标识位,标明执行的操作正在处理中。这个属性是配合graceTime使用的。// 如果没有设置graceTime,则这个标识是没有太大意义的。在使用showWhileExecuting:onTarget:withObject:animated:方法时,// 会自动去设置这个属性为YES,其它情况下都需要我们自己手动设置。 @property (assign) BOOL taskInProgress; *///17,设置隐藏的时候是否从父视图中移除,默认是NO HUD.removeFromSuperViewOnHide = NO;//18,进度指示器 模式是0,取值从0.0————1.0// HUD.progress = 0.5;//19,隐藏时候的回调 隐藏动画结束之后
HUD.completionBlock = ^(){
NSLog(@"abnnfsfsf");
};
//设置任务,在hud上显示任务的进度
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
// [HUD show:YES];
//两种隐藏的方法
// [HUD hide:YES];
[HUD hide:YES afterDelay:5];
}
//任务,测试进度显示
- (void)myProgressTask {// This just increases the progress indicator in a loopfloat progress =0.0f; while (progress <1.0f) { progress +=0.01f; HUD.progress = progress; usleep(50000);
}
}
MBProgressHUD 使用详解
1,MBProgressHUDMode枚举
MBProgressHUD中定义的MBProgressHUDMode枚举。它用来表示HUD窗口的模式,即我们从效果图中看到的几种显示样式。
typedefenum { // 使用UIActivityIndicatorView来显示进度,这是默认值 MBProgressHUDModeIndeterminate, // 使用一个圆形饼图来作为进度视图 MBProgressHUDModeDeterminate, // 使用一个水平进度条 MBProgressHUDModeDeterminateHorizontalBar, // 使用圆环作为进度条 MBProgressHUDModeAnnularDeterminate, // 显示一个自定义视图,通过这种方式,可以显示一个正确或错误的提示图 MBProgressHUDModeCustomView, // 只显示文本
MBProgressHUDModeText
} MBProgressHUDMode;
2,MBProgressHUD的基本组成:
A,loading动画:就是MBProgressHUD上部分显示的动画效果的一部分,这个区域也可以使用我们自己设定的UIImageView视图,这个视图显示内容由我们设置的模式来决定,可以是菊花,进度条,圆环,也可以我们自定义视图。
B,标题文本:主要用于显示提示信息。这个文本框是可选的,通常位于loading动画的下面,且是单行显示。它会根据labelText属性来自适应文本的大小,有上限,过长会显示“…”。
C,详情文本框。如果觉得标题不够详细,这里可以添加附属信息,可以将详细信息放到这里,该文本框显示的是detailLabelText属性的值,它是多行显示的。另外详情显示还依赖于labelText属性的值,只有labelText属性被设置了,并且不是空串,才会显示detailsLabel。
D,HUD背景框:主要作用是上面三个部分的一个背景,用来突出显示上面三部分。
3,MBProgressHUD 常用属性
MBProgressHUD几个常用的属性:
// 背景框的透明度,默认值是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 属性,由中心向MBProgressHUD视图的四周绘制了一个渐变层。@property (assign)BOOL dimBackground;//设置 MBProgressHUD 显示的动画模式@property (assign) MBProgressHUDMode mode;
控制HUD的布局的几个属性:
// 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是显示或者隐藏时候提供的属性:
// 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;
注意:
1),MBProgressHUD视图会充满其父视图的frame内,因此,当MBProgressHUD显示时,它也会屏蔽父视图的各种交互操作。
2)自定义loading动画视图,此时选择的模式是MBProgressHUDModeCustomView。或者不显示loading动画视图,而只显示文本框(MBProgressHUDModeText)。
3)对于HUD的隐藏,MBProgressHUD提供了两个方法,一个是-hide:,另一个是-hide:afterDelay:,后者基于前者
4)MBProgressHUD还为我们提供了几个便捷显示和隐藏HUD窗口的方法
+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated5)MBProgressHUD还为我们提供了一个代理MBProgressHUDDelegate,这个代理中只提供了一个方法,即:- (void)hudWasHidden:(MBProgressHUD *)hud;//隐藏HUD后调用的方法
网友评论