MBProgressHUD框架的使用

来源:互联网 发布:淘宝异地招聘客服贴吧 编辑:程序博客网 时间:2024/06/06 04:43

MBProgressHUD框架是一个刷新的框架,再不占据主线程的情况下,开发一个新线程,具体用法如下:

1.

//刷新

-(void)loading:(NSString *)title{

   if (hud!=nil) {

        [hudremoveFromSuperview];

    }

    hud=[[MBProgressHUDalloc]initWithWindow:[UIApplicationsharedApplication].keyWindow];

    [[UIApplicationsharedApplication].keyWindowaddSubview:hud];

    hud.mode=MBProgressHUDModeIndeterminate;

    hud.delegate=self;

    hud.detailsLabelText=title;

    [hudshow:YES];

}


//停止刷新

-(void)loadhidden:(float)time{

    [selfperformSelector:@selector(hide)withObject:selfafterDelay:time];

}

-(void)hide{

    [hudhide:YES];

}

//清除缓存

- (void)hudWasHidden:(MBProgressHUD *)theHud {

    [hudremoveFromSuperview];

}


2.或者是

- (IBAction)showSimple:(id)sender {

        //单纯刷新

// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

// Regiser for HUD callbacks so we can remove it from the window at the right time

HUD.delegate =self;

// Show the HUD while the provided method executes in a new thread

[HUDshowWhileExecuting:@selector(myTask)onTarget:selfwithObject:nilanimated:YES];

        

        //带标题

         HUD.labelText =@"Loading";

        //副标题

HUD.detailsLabelText =@"updating data";

HUD.square =YES;

        //调节模式

        HUD.mode =MBProgressHUDModeDeterminate;(闹钟转动)

        //显示,隐藏

[HUDshow:YES];

[HUDhide:YESafterDelay:3];


}







@interface HudDemoViewController () <MBProgressHUDDelegate> {

MBProgressHUD *HUD;

long long expectedLength;

long long currentLength;

}

- (IBAction)showSimple:(id)sender {

// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

// Regiser for HUD callbacks so we can remove it from the window at the right time

HUD.delegate =self;

// Show the HUD while the provided method executes in a new thread

[HUDshowWhileExecuting:@selector(myTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showWithLabel:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

HUD.delegate =self;

HUD.labelText =@"Loading";

[HUDshowWhileExecuting:@selector(myTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showWithDetailsLabel:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

HUD.delegate =self;

HUD.labelText =@"Loading";

HUD.detailsLabelText =@"updating data";

HUD.square =YES;

[HUDshowWhileExecuting:@selector(myTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showWithLabelDeterminate:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

// Set determinate mode

HUD.mode =MBProgressHUDModeDeterminate;

HUD.delegate =self;

HUD.labelText =@"Loading";

// myProgressTask uses the HUD instance to update progress

[HUDshowWhileExecuting:@selector(myProgressTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showWIthLabelAnnularDeterminate:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

// Set determinate mode

HUD.mode =MBProgressHUDModeAnnularDeterminate;

HUD.delegate =self;

HUD.labelText =@"Loading";

// myProgressTask uses the HUD instance to update progress

[HUDshowWhileExecuting:@selector(myProgressTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showWithLabelDeterminateHorizontalBar:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

// Set determinate bar mode

HUD.mode =MBProgressHUDModeDeterminateHorizontalBar;

HUD.delegate =self;

// myProgressTask uses the HUD instance to update progress

[HUDshowWhileExecuting:@selector(myProgressTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showWithCustomView:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

// The sample image is based on the work byhttp://www.pixelpressicons.com,http://creativecommons.org/licenses/by/2.5/ca/

// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)

HUD.customView = [[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"37x-Checkmark.png"]]autorelease];

// Set custom view mode

HUD.mode =MBProgressHUDModeCustomView;

HUD.delegate =self;

HUD.labelText =@"Completed";

[HUDshow:YES];

[HUDhide:YESafterDelay:3];

}


- (IBAction)showWithLabelMixed:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

HUD.delegate =self;

HUD.labelText =@"Connecting";

HUD.minSize =CGSizeMake(135.f,135.f);

[HUDshowWhileExecuting:@selector(myMixedTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showUsingBlocks:(id)sender {

#if NS_BLOCKS_AVAILABLE

MBProgressHUD *hud = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:hud];

hud.labelText =@"With a block";

[hud showAnimated:YESwhileExecutingBlock:^{

[selfmyTask];

} completionBlock:^{

[hud removeFromSuperview];

[hudrelease];

}];

#endif

}


- (IBAction)showOnWindow:(id)sender {

// The hud will dispable all input on the window

HUD = [[MBProgressHUDalloc]initWithView:self.view.window];

[self.view.windowaddSubview:HUD];

HUD.delegate =self;

HUD.labelText =@"Loading";

[HUDshowWhileExecuting:@selector(myTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showURL:(id)sender {

NSURL *URL = [NSURLURLWithString:@"http://a1408.g.akamai.net/5/1408/1388/2005110403/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_iPod.m4v.zip"];

NSURLRequest *request = [NSURLRequestrequestWithURL:URL];

NSURLConnection *connection = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];

[connectionstart];

[connectionrelease];

HUD = [[MBProgressHUDshowHUDAddedTo:self.navigationController.viewanimated:YES]retain];

HUD.delegate =self;

}



- (IBAction)showWithGradient:(id)sender {

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

HUD.dimBackground =YES;

// Regiser for HUD callbacks so we can remove it from the window at the right time

HUD.delegate =self;

// Show the HUD while the provided method executes in a new thread

[HUDshowWhileExecuting:@selector(myTask)onTarget:selfwithObject:nilanimated:YES];

}


- (IBAction)showTextOnly:(id)sender {

MBProgressHUD *hud = [MBProgressHUDshowHUDAddedTo:self.navigationController.viewanimated:YES];

// Configure for text only and offset down

hud.mode =MBProgressHUDModeText;

hud.labelText =@"Some message...";

hud.margin =10.f;

hud.removeFromSuperViewOnHide =YES;

[hud hide:YESafterDelay:3];

}


- (IBAction)showWithColor:(id)sender{

HUD = [[MBProgressHUDalloc]initWithView:self.navigationController.view];

[self.navigationController.viewaddSubview:HUD];

// Set the hud to display with a color

HUD.color = [UIColorcolorWithRed:0.23green:0.50blue:0.82alpha:0.90];

HUD.delegate =self;

[HUDshowWhileExecuting:@selector(myTask)onTarget:selfwithObject:nilanimated:YES];

}


#pragma mark - Execution code


- (void)myTask {

// Do something usefull in here instead of sleeping ...

sleep(3);

}


- (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)myMixedTask {

// Indeterminate mode

sleep(2);

// Switch to determinate mode

HUD.mode =MBProgressHUDModeDeterminate;

HUD.labelText =@"Progress";

float progress =0.0f;

while (progress <1.0f)

{

progress +=0.01f;

HUD.progress = progress;

usleep(50000);

}

// 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

__blockUIImageView *imageView;

dispatch_sync(dispatch_get_main_queue(), ^{

UIImage *image = [UIImageimageNamed:@"37x-Checkmark.png"];

imageView = [[UIImageViewalloc]initWithImage:image];

});

HUD.customView = [imageViewautorelease];

HUD.mode =MBProgressHUDModeCustomView;

HUD.labelText =@"Completed";

sleep(2);

}


#pragma mark - NSURLConnectionDelegete


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

expectedLength =MAX([responseexpectedContentLength],1);

currentLength =0;

HUD.mode =MBProgressHUDModeDeterminate;

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

currentLength += [datalength];

HUD.progress =currentLength / (float)expectedLength;

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

HUD.customView = [[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"37x-Checkmark.png"]]autorelease];

HUD.mode =MBProgressHUDModeCustomView;

[HUDhide:YESafterDelay:2];

}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[HUDhide:YES];

}


3.//属性

#pragma mark - MBProgressHUDDelegate


- (void)hudWasHidden:(MBProgressHUD *)hud {

// Remove HUD from screen when the HUD was hidded

[HUDremoveFromSuperview];

[HUDrelease];

HUD =nil;

}


#pragma mark - Properties


@synthesize animationType;

@synthesize delegate;

@synthesize opacity;

@synthesize color;

@synthesize labelFont;

@synthesize labelColor;

@synthesize detailsLabelFont;

@synthesize detailsLabelColor;

@synthesize indicator;

@synthesize xOffset;

@synthesize yOffset;

@synthesize minSize;

@synthesize square;

@synthesize margin;

@synthesize dimBackground;

@synthesize graceTime;

@synthesize minShowTime;

@synthesize graceTimer;

@synthesize minShowTimer;

@synthesize taskInProgress;

@synthesize removeFromSuperViewOnHide;

@synthesize customView;

@synthesize showStarted;

@synthesize mode;

@synthesize labelText;

@synthesize detailsLabelText;

@synthesize progress;

@synthesize size;

@synthesize activityIndicatorColor;

#if NS_BLOCKS_AVAILABLE

@synthesize completionBlock;

#endif





0 0
原创粉丝点击