IOS第三方之MBProgressHUD

//
//  ViewController.m
//  MBProgressHUD
//
//  Created by City--Online on 15/6/15.
//  Copyright (c) 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"
#import "MBProgressHUD.h"

@interface ViewController ()<MBProgressHUDDelegate,NSURLConnectionDataDelegate>
@property(nonatomic,strong) MBProgressHUD *hud;
@property(nonatomic,assign) long long expectedLength;
@property(nonatomic,assign) long long currentLength;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //普通提示框
    UIButton *simpleBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    [simpleBtn setTitle:@"普通" forState:UIControlStateNormal];
    [simpleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    simpleBtn.frame=CGRectMake(20, 70, 100, 100);
    simpleBtn.backgroundColor=[UIColor redColor];
    simpleBtn.tag=10001;
    [self.view addSubview:simpleBtn];

    //显示进度
    UIButton *progressBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    [progressBtn setTitle:@"进度" forState:UIControlStateNormal];
    [progressBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    progressBtn.frame=CGRectMake(130, 70, 100, 100);
    progressBtn.backgroundColor=[UIColor redColor];
    progressBtn.tag=10002;
    [self.view addSubview:progressBtn];

    //自定义视图
    UIButton *customBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    [customBtn setTitle:@"自定义" forState:UIControlStateNormal];
    [customBtn addTarget:self action:@selector(customClick:) forControlEvents:UIControlEventTouchUpInside];
    customBtn.frame=CGRectMake(20, 180, 100, 100);
    customBtn.backgroundColor=[UIColor redColor];
    customBtn.tag=10003;
    [self.view addSubview:customBtn];
    //Window视图
    UIButton *windowBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    [windowBtn setTitle:@"Window" forState:UIControlStateNormal];
    [windowBtn addTarget:self action:@selector(windowClick:) forControlEvents:UIControlEventTouchUpInside];
    windowBtn.frame=CGRectMake(130, 180, 100, 100);
    windowBtn.backgroundColor=[UIColor redColor];
    windowBtn.tag=10004;
    [self.view addSubview:windowBtn];

    //多提示框
    UIButton *mixBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    [mixBtn setTitle:@"多提示框" forState:UIControlStateNormal];
    [mixBtn addTarget:self action:@selector(mixClick:) forControlEvents:UIControlEventTouchUpInside];
    mixBtn.frame=CGRectMake(20, 290, 100, 100);
    mixBtn.backgroundColor=[UIColor redColor];
    mixBtn.tag=10004;
    [self.view addSubview:mixBtn];

    //多提示框
    UIButton *connectionBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    [connectionBtn setTitle:@"Connection" forState:UIControlStateNormal];
    [connectionBtn addTarget:self action:@selector(connectionClick:) forControlEvents:UIControlEventTouchUpInside];
    connectionBtn.frame=CGRectMake(130, 290, 100, 100);
    connectionBtn.backgroundColor=[UIColor redColor];
    connectionBtn.tag=10004;
    [self.view addSubview:connectionBtn];

}
//普通
-(void)btnClick:(id)sender
{
    UIButton *btn=(UIButton *)sender;

    _hud=[[MBProgressHUD alloc]initWithView:self.view];
    _hud.delegate=self;
    _hud.labelText=@"Loading";
    _hud.detailsLabelText=@"updating data";
    _hud.square=YES;
    //默认
    _hud.mode=MBProgressHUDModeIndeterminate;
    if (btn.tag==10002) {
        _hud.mode=MBProgressHUDModeDeterminateHorizontalBar;
    }
    [self.view addSubview:_hud];

    [_hud showAnimated:YES whileExecutingBlock:^{
        if (btn.tag==10001) {
             sleep(3);
        }
        else if(btn.tag==10002)
        {
            [self myProgressTask];
        }

    } completionBlock:^{
        [_hud removeFromSuperview];
    }];

}
//自定义视图
-(void)customClick:(id)sender
{
    _hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_hud];
    // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
    _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];

    // Set custom view mode
    _hud.mode = MBProgressHUDModeCustomView;

    _hud.delegate = self;
    _hud.labelText=@"Loading";
    _hud.detailsLabelText=@"updating data";
    _hud.square=YES;

    [_hud show:YES];
    [_hud hide:YES afterDelay:3];

}

//window视图
-(void)windowClick:(id)sender
{
    _hud=[[MBProgressHUD alloc]initWithWindow:self.view.window];
    [self.view.window addSubview:_hud];
    _hud.delegate=self;
    _hud.labelText=@"Loading";
    [_hud showWhileExecuting:@selector(task) onTarget:self withObject:nil animated:YES];
}
//多提示框
-(void)mixClick:(id)sender
{
    _hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_hud];

    _hud.delegate = self;
    _hud.labelText = @"Connecting";
//    _hud.minSize = CGSizeMake(135.f, 135.f);

    [_hud showWhileExecuting:@selector(mixedTask) onTarget:self withObject:nil animated:YES];

}
-(void)connectionClick:(id)sender
{
    NSURL *url=[NSURL URLWithString:@"http://a1408.g.akamai.net/5/1408/1388/2005110403/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_iPod.m4v.zip"];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    [connection start];
    _hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _hud.delegate=self;
     _hud.mode=MBProgressHUDModeDeterminate;

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    _expectedLength=MAX(response.expectedContentLength, 1);
    _currentLength=0;

}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    _currentLength+=[data length];
    _hud.progress=_currentLength/(float)_expectedLength;

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    _hud.customView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    _hud.mode=MBProgressHUDModeCustomView;
    [_hud hide:YES afterDelay:2];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [_hud hide:YES];
}
//显示进度
- (void)myProgressTask {
    // This just increases the progress indicator in a loop
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        _hud.progress = progress;
        usleep(50000);
    }

}
-(void)task
{
    sleep(3);
}
-(void)mixedTask
{
    _hud.mode=MBProgressHUDModeIndeterminate;
    _hud.labelText=@"Progressing";
    float progress=0.0f;
    while (progress<1.0f) {
        progress+=0.1f;
        _hud.progress=progress;
        usleep(5000);
    }
    // Back to indeterminate mode
    _hud.mode = MBProgressHUDModeIndeterminate;
    _hud.labelText = @"Cleaning up";
    sleep(2);
    // UIImageView is a UIKit class, we have to initialize it on the main thread
    __block UIImageView *imageView;
    dispatch_sync(dispatch_get_main_queue(), ^{
        UIImage *image = [UIImage imageNamed:@"37x-Checkmark.png"];
        imageView = [[UIImageView alloc] initWithImage:image];
    });
    _hud.customView =imageView;
    _hud.mode = MBProgressHUDModeCustomView;
    _hud.labelText = @"Completed";
    sleep(2);
}
- (void)hudWasHidden:(MBProgressHUD *)hud {
    // Remove HUD from screen when the HUD was hidded
    [_hud removeFromSuperview];
    _hud=nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

时间: 2024-10-25 01:12:21

IOS第三方之MBProgressHUD的相关文章

IOS 第三方框架-MBProgressHUD

IOS 第三方框架-MBProgressHUD MBProgressHUD提示框官网地址:https://github.com/jdg/MBProgressHUD 官网里已经提供了足够多的例子供我们使用,但在实现开发中,我们用到的只是其中的一小部分而已.为了使用更方便,下面对它进行扩展(Category) MBProgressHUD+NJ.h #import "MBProgressHUD.h" @interface MBProgressHUD (NJ) + (void)showSucc

超全!整理常用的iOS第三方资源

超全!整理常用的iOS第三方资源 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github.com/jdg/MBProgressHUD 3:XML/HTML解析 地址:https://github.com/topfunky/hpple 4:有文字输入时,能根据键盘是否弹出来调整自身显示内容的位置 地址:https://github.com/michaelt

iOS开发——使用MBProgressHUD来增加用户体验(二)

我在上一篇博客<iOS开发--使用MBProgressHUD来增加用户体验>主要实现了使用别人已经封装的MBProgressHUD来进行加载提示,可以说是相当的方便.今天我们使用Github上原生的MBProgressHUD第三方库来进行加载提示,会比别人已经封装的麻烦一点点.代码已经上传至:https://github.com/chenyufeng1991/UseMBProgressHUD.实现步骤如下: (1)同样是使用网络请求号码归属地来实现.(请看注释) - (IBAction)sou

&lt;转&gt;iOS第三方开源库的吐槽和备忘

iOS第三方开源库的吐槽和备忘 做iOS开发总会接触到一些第三方库,这里整理一下,做一些吐槽. 目前比较活跃的社区仍旧是Github,除此以外也有一些不错的库散落在Google Code.SourceForge等地方.由于Github社区太过主流,这里主要介绍一下Github里面流行的iOS库. 首先整理了一份Github上排名靠前的iOS库(大概600个repos) 除了逛一下每日/每月流行之外,也可以到这里来看一下整个iOS Repos的排名. 下面是一些比较流行的第三方库: HTTP 相比

超全!整理常用的iOS第三方资源(转)

超全!整理常用的iOS第三方资源 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github.com/jdg/MBProgressHUD 3:XML/HTML解析 地址:https://github.com/topfunky/hpple 4:有文字输入时,能根据键盘是否弹出来调整自身显示内容的位置 地址:https://github.com/michaelt

iOS第三方库汇总[转载]

iOS第三方库汇总[转载] 字数2179 阅读334 评论0 喜欢29 简介 此文用于总结,本人使用过或者收藏过的Github第三方类库,以便日后查阅,也便他人借鉴. 资料整理中不定期更新... 开源项目 CodeHub browse and maintain your GitHub repositories on any iOS device! Open-Source iOS Apps 开源iOS apps列表 APP相关 iVersion 提示版本更新 BonMot 字体相关的库,设置字体样

iOS 第三方库、插件、知名博客总结

用到的组件1.通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SDWebImage多个缩略图缓存组件 UICKeyChainStore存放用户账号密码组件 Reachability监测网络状态 DateTools友好化时间 MBProgressHUD一款提示框第三方库 MWPhotoBrowser一款简单的 iOS 照片浏览控件 CTAssetsPickerController一个选择器组件, 支持从用户的相片库选择多张照片和视频. QB

IOS第三方框架集合-02

IOS第三方框架集合 Reachability 检测网络连接 用来检查网络连接是否可用:包括WIFI和WWAN(3G/EDGE/CDMA等)两种工作模式. 现在有更好的替代品:https://github.com/tonymillion/Reachability,比Apple提供的兼容性更好,而且更加好用,更具体的使用方法请看它提供的例子. Reachability reach = [Reachability reachabilityWithHostname:@"www.google.com&q

iOS第三方控件

一.SIAlertView https://github.com/Sumi-Interactive/SIAlertView 感言: 扁平化设计的对话框(UIAlertView),对话框的弹出与消失的动画很不错,可以自定义对话框的外观 iOS第三方控件