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版本切换工具pyenv的安装及用法

《python版本切换工具pyenv的安装及用法》Pyenv是管理Python版本的最佳工具之一,特别适合开发者和需要切换多个Python版本的用户,:本文主要介绍python版本切换工具pyen... 目录Pyenv 是什么?安装 Pyenv(MACOS)使用 Homebrew:配置 shell(zsh

Java中的CompletableFuture核心用法和常见场景

《Java中的CompletableFuture核心用法和常见场景》CompletableFuture是Java8引入的强大的异步编程工具,支持链式异步编程、组合、异常处理和回调,介绍其核心用法,通过... 目录1、引言2. 基本概念3. 创建 CompletableFuture3.1. 手动创建3.2.

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

MySQL基本表查询操作汇总之单表查询+多表操作大全

《MySQL基本表查询操作汇总之单表查询+多表操作大全》本文全面介绍了MySQL单表查询与多表操作的关键技术,包括基本语法、高级查询、表别名使用、多表连接及子查询等,并提供了丰富的实例,感兴趣的朋友跟... 目录一、单表查询整合(一)通用模版展示(二)举例说明(三)注意事项(四)Mapper简单举例简单查询

Java序列化之serialVersionUID的用法解读

《Java序列化之serialVersionUID的用法解读》Java序列化之serialVersionUID:本文介绍了Java对象的序列化和反序列化过程,强调了serialVersionUID的作... 目录JavChina编程a序列化之serialVersionUID什么是序列化为什么要序列化serialV

python3中正则表达式处理函数用法总结

《python3中正则表达式处理函数用法总结》Python中的正则表达式是一个强大的文本处理工具,用于匹配、查找、替换等操作,在Python中正则表达式的操作主要通过内置的re模块来实现,这篇文章主要... 目录前言re.match函数re.search方法re.match 与 re.search的区别检索

MySQL 中的 JSON_CONTAIN用法示例详解

《MySQL中的JSON_CONTAIN用法示例详解》JSON_CONTAINS函数用于检查一个JSON文档中是否包含另一个JSON文档,这篇文章给大家介绍JSON_CONTAINS的用法、语法、... 目录深入了解 mysql 中的 jsON_CONTAINS1. JSON_CONTAINS 函数的概述2

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Java8 Collectors.toMap() 的两种用法

《Java8Collectors.toMap()的两种用法》Collectors.toMap():JDK8中提供,用于将Stream流转换为Map,本文给大家介绍Java8Collector... 目录一、简单介绍用法1:根据某一属性,对对象的实例或属性做映射用法2:根据某一属性,对对象集合进行去重二、Du