zoukankan      html  css  js  c++  java
  • 【开源类库学习】MBProgressHUD(提示框)

    新博客:

    http://www.liuchendi.com

    MBProgressHUD是一个开源类库,实现了各种样式的提示框, 下载地址:https://github.com/jdg/MBProgressHUD,然后把两个MBProgressHUD.h和MBProgressHUD.m放到自己的项目就可以了。这里有一个小Demo可以参考一下。

    头文件部分:

    #import <UIKit/UIKit.h>
    #import "MBProgressHUD.h"
    
    @interface ViewController : UIViewController
    {
        
        MBProgressHUD *HUD;
    }
    
    - (IBAction)showTextDialog:(id)sender;      //文本提示框,默认情况下
    - (IBAction)showProgressOne:(id)sender;     //第一种加载提示框
    - (IBAction)showProgressTwo:(id)sender;     //第二种加载提示框
    - (IBAction)showProgressThree:(id)sender;   //第三种加载提示框
    - (IBAction)showCustomDialog:(id)sender;    //自定义提示框,显示打钩效果
    - (IBAction)showAllTextDialog:(id)sender;   //显示纯文本提示框
    
    @end

    实现文件部分

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)showTextDialog:(id)sender {
        
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        
        HUD.dimBackground = YES;  //把当前的view置于后台
        HUD.labelText = @"请稍等";
        
        //显示对话框
        [HUD showAnimated:YES whileExecutingBlock:^{
         
            sleep(3);
        } completionBlock:^{
        }];
    }
    
    - (IBAction)showProgressOne:(id)sender {
        
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        
        HUD.labelText = @"正在加载";
        HUD.mode = MBProgressHUDModeDeterminate;
        //HUD.mode = MBProgressHUDModeAnnularDeterminate;
        [HUD showAnimated:YES whileExecutingBlock:^{
            
            float progress = 0.0f;
            while (progress < 1.0f) {
                progress += 0.01f;
                HUD.progress = progress;
                usleep(5000);
            }
        } completionBlock:^{
            [HUD removeFromSuperview];
            [HUD release];
            HUD = nil;
    
        }];
    }
    
    
    
    - (IBAction)showProgressTwo:(id)sender {
    
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        
        HUD.labelText = @"正在加载";
        HUD.mode = HUD.mode = MBProgressHUDModeAnnularDeterminate;
        [HUD showAnimated:YES whileExecutingBlock:^{
            
            float progress = 0.0f;
            while (progress < 1.0f) {
                progress += 0.01f;
                HUD.progress = progress;
                usleep(5000);
            }
        } completionBlock:^{
            [HUD removeFromSuperview];
            [HUD release];
            HUD = nil;
            
        }];
    
    }
    
    - (IBAction)showProgressThree:(id)sender {
        
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        
        HUD.labelText = @"正在加载";
        HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
        [HUD showAnimated:YES whileExecutingBlock:^{
            
            float progress = 0.0f;
            while (progress < 1.0f) {
                progress += 0.01f;
                HUD.progress = progress;
                usleep(5000);
            }
        } completionBlock:^{
            [HUD removeFromSuperview];
            [HUD release];
            HUD = nil;
            
        }];
    
    }
    
    - (IBAction)showCustomDialog:(id)sender {
        
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        
        HUD.labelText = @"操作成功";
        HUD.mode = MBProgressHUDModeCustomView;
        HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];
        [HUD showAnimated:YES whileExecutingBlock:^{
            sleep(2);
        } completionBlock:^{
            [HUD removeFromSuperview];
            [HUD release];
            HUD = nil;
        }];
    }
    
    - (IBAction)showAllTextDialog:(id)sender {
        
        
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        
        HUD.labelText = @"操作成功";
        HUD.mode = MBProgressHUDModeText;
        HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];
        [HUD showAnimated:YES whileExecutingBlock:^{
            sleep(2);
        } completionBlock:^{
            [HUD removeFromSuperview];
            [HUD release];
            HUD = nil;
        }];
        
    }
    
    - (void)dealloc {
    
        [super dealloc];
    }
    @end

    实现效果如图所示:

    1、默认效果,也就是MBProgressHUDModeIndeterminate

    2、第一种加载提示框,MBProgressHUDModeDeterminate

    3、第二种加载提示MBProgressHUDModeAnnularDeterminate

    4、第三种加载提示框,MBProgressHUDModeDeterminateHorizontalBar

    5、自定义提示框 ,可以带图片的MBProgressHUDModeCustomView

    6.纯文本提示框

    如果有什么问题,欢迎通过微博交流 @Linux_小木头

  • 相关阅读:
    转载:Python十分钟入门
    Think Python: How to Think Like a Computer Scientist
    LeetCode(34):搜索范围
    LeetCode(33):搜索旋转排序数组
    LeetCode(32):最长有效括号
    LeetCode(31): 下一个排列
    LeetCode(30):与所有单词相关联的字串
    LeetCode(29): 两数相除
    LeetCode(28): 实现strStr()
    LeetCode(27): 移除元素
  • 原文地址:https://www.cnblogs.com/iOS-dd/p/3416408.html
Copyright © 2011-2022 走看看