Using SCLAlert as HUD

In your class.h file interface:

SCLAlertView *hud;
NSString *hudTitle;
NSString *hudText;

In your class.m file provide these methods:

 -(void)startHud {

    [self.view endEditing:YES]; //make sure keyboard is down

    hud = [[SCLAlertView alloc] init];
    [hud setShowAnimationType:SlideInToCenter];
    hud.backgroundType = Blur;
    hud.customViewColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.7f]; //set custom color
    [hud setHideAnimationType:SlideOutFromCenter];
    [hud showWaiting:self title:hudTitle
    subTitle:hudText
    closeButtonTitle:nil duration:0];
}

-(void)stopHud {
    [hud setHideAnimationType:SlideOutToCenter];
    hudText = @"";
    hudText = @"";
    [hud hideView];
}

Now wherever you want to invoke the spinner simply:

                    hudTitle = NSLocalizedString(@"Processing Authorization", nil);
                    hudText = NSLocalizedString(@"Communicating with host...", nil);
                    [self startHud];

Wherever you want to hide the spinner:

                   [self stopHud];

Taking this a step further combining with SCAlerts:

                [alert addButton:NSLocalizedString(@"Authorization", nil) actionBlock:^{
                    hudTitle = NSLocalizedString(@"Processing Authorization", nil);
                    hudText = NSLocalizedString(@"Communicating with host...", nil);
                    [self saveObject];   //invoke some method
                    return;
                }];

                [alert addButton:NSLocalizedString(@"Update Application", nil) actionBlock:^{
                    hudTitle = NSLocalizedString(@"Update Application", nil);
                    hudText = NSLocalizedString(@"Communicating with host...", nil);
                    [self updateObject];   //invoke some other method
                    return;
                }];

in your method (saveObject);

-(void)saveObject {
    [self startHud];
    // do something
    [self stopHud];
}

关于Zeno Chen

本人涉及的领域较多,杂而不精 程序设计语言: Perl, Java, PHP, Python; 数据库系统: MySQL,Oracle; 偶尔做做电路板的开发,主攻STM32单片机
此条目发表在Objective-C分类目录。将固定链接加入收藏夹。