iOS MBProgressHUD的基本用法

2024-05-31 10:58

本文主要是介绍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) {

// 默认效果,只有透明度变化的动画效果 MBProgressHUDAnimationFade, 
// 透明度变化+形变效果,其中MBProgressHUDAnimationZoom和
// MBProgressHUDAnimationZoomOut的枚举值都为1 
MBProgressHUDAnimationZoom, 
MBProgressHUDAnimationZoomOut = MBProgressHUDAnimationZoom, 
MBProgressHUDAnimationZoomIn

};




// 背景框的透明度,默认值是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.直接在Viewshow

  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;//整个MBViewY的位置(起点位置是在view的中心附近)

    hud.xOffset=10.0;//整个MBViewX的位置

    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;//将序列帧数组赋给UIImageViewanimationImages属性

    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;//整个MBViewY的位置(起点位置是在view的中心附近)

    hud.xOffset=10.0;//整个MBViewX的位置

    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:这个viewbounds就是HUDbounds

 */

- (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的基本用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1017779

相关文章

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

C 语言的基本数据类型

C 语言的基本数据类型 注:本文面向 C 语言初学者,如果你是熟手,那就不用看了。 有人问我,char、short、int、long、float、double 等这些关键字到底是什么意思,如果说他们是数据类型的话,那么为啥有这么多数据类型呢? 如果写了一句: int a; 那么执行的时候在内存中会有什么变化呢? 橡皮泥大家都玩过吧,一般你买橡皮泥的时候,店家会赠送一些模板。 上

FreeRTOS-基本介绍和移植STM32

FreeRTOS-基本介绍和STM32移植 一、裸机开发和操作系统开发介绍二、任务调度和任务状态介绍2.1 任务调度2.1.1 抢占式调度2.1.2 时间片调度 2.2 任务状态 三、FreeRTOS源码和移植STM323.1 FreeRTOS源码3.2 FreeRTOS移植STM323.2.1 代码移植3.2.2 时钟中断配置 一、裸机开发和操作系统开发介绍 裸机:前后台系

Java 多线程的基本方式

Java 多线程的基本方式 基础实现两种方式: 通过实现Callable 接口方式(可得到返回值):

UVM:callback机制的意义和用法

1. 作用         Callback机制在UVM验证平台,最大用处就是为了提高验证平台的可重用性。在不创建复杂的OOP层次结构前提下,针对组件中的某些行为,在其之前后之后,内置一些函数,增加或者修改UVM组件的操作,增加新的功能,从而实现一个环境多个用例。此外还可以通过Callback机制构建异常的测试用例。 2. 使用步骤         (1)在UVM组件中内嵌callback函

Java基础回顾系列-第一天-基本语法

基本语法 Java基础回顾系列-第一天-基本语法基础常识人机交互方式常用的DOS命令什么是计算机语言(编程语言) Java语言简介Java程序运行机制Java虚拟机(Java Virtual Machine)垃圾收集机制(Garbage Collection) Java语言的特点面向对象健壮性跨平台性 编写第一个Java程序什么是JDK, JRE下载及安装 JDK配置环境变量 pathHe