• 【开源类库学习】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_小木头

  • 相关阅读:
    用Canvas绘制一个钟表
    用css3做一个3D立方体
    函数调用的不同方式,以及this的指向
    Javascript 严格模式use strict详解
    前端开发页面的性能优化方案总结
    Promise对象解读
    Vue爬坑之vuex初识
    WEB前端性能优化小结
    navicat 注册码
    docker
  • 原文地址:https://www.cnblogs.com/iOS-dd/p/3416408.html
Copyright © 2020-2023  润新知