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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创