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

相关文章

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

java中Optional的核心用法和最佳实践

《java中Optional的核心用法和最佳实践》Java8中Optional用于处理可能为null的值,减少空指针异常,:本文主要介绍java中Optional核心用法和最佳实践的相关资料,文中... 目录前言1. 创建 Optional 对象1.1 常规创建方式2. 访问 Optional 中的值2.1

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

git stash命令基本用法详解

《gitstash命令基本用法详解》gitstash是Git中一个非常有用的命令,它可以临时保存当前工作区的修改,让你可以切换到其他分支或者处理其他任务,而不需要提交这些还未完成的修改,这篇文章主要... 目录一、基本用法1. 保存当前修改(包括暂存区和工作区的内容)2. 查看保存了哪些 stash3. 恢

Python struct.unpack() 用法及常见错误详解

《Pythonstruct.unpack()用法及常见错误详解》struct.unpack()是Python中用于将二进制数据(字节序列)解析为Python数据类型的函数,通常与struct.pa... 目录一、函数语法二、格式字符串详解三、使用示例示例 1:解析整数和浮点数示例 2:解析字符串示例 3:解

C++/类与对象/默认成员函数@构造函数的用法

《C++/类与对象/默认成员函数@构造函数的用法》:本文主要介绍C++/类与对象/默认成员函数@构造函数的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录名词概念默认成员函数构造函数概念函数特征显示构造函数隐式构造函数总结名词概念默认构造函数:不用传参就可以

javascript fetch 用法讲解

《javascriptfetch用法讲解》fetch是一个现代化的JavaScriptAPI,用于发送网络请求并获取资源,它是浏览器提供的全局方法,可以替代传统的XMLHttpRequest,这篇... 目录1. 基本语法1.1 语法1.2 示例:简单 GET 请求2. Response 对象3. 配置请求