提交信息等待网络请求完成或者某一个条件允许的情况下,我们会在屏幕上或者按钮上面添加一个菊花,表示当前正处于等待状态,下面看看效果图,以下效果图是结合上一篇倒计时方法写的,具体的实现,待会儿一起看看代码:
菊花按钮.gif
调用代码:
#pragma mark ---开始倒计时点击事件
-(void)startAction{
NSInteger time =5;
[_endBt setTitle:[NSString stringWithFormat:@"%zd",time] forState:UIControlStateNormal];
[_startBt showIndicator];//开始菊花转起来
[_startBt startTime:time waitBlock:^(NSInteger remainTime) {
DLog(@"%zd",remainTime);
[_endBt setTitle:[NSString stringWithFormat:@"%zd",remainTime] forState:UIControlStateNormal];
} finishBlock:^{
[_startBt hideIndicator];//菊花消失
DLog(@"finishBlock");
[_endBt setTitle:@"倒计时结束" forState:UIControlStateNormal];
}];
}
UIButton+Indicator.h
/** 提交按钮: 提交时在中间显示一个菊花 */
@interface UIButton (Indicator)
/** 显示菊花 */
- (void)showIndicator;
/** 隐藏菊花 */
- (void)hideIndicator;
@end
UIButton+Indicator.m
#import <objc/runtime.h>
static NSString *const IndicatorViewKey = @"indicatorView";
static NSString *const ButtonTextObjectKey = @"buttonTextObject";
@implementation UIButton (Indicator)
- (void)showIndicator
{
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
[indicator startAnimating];
NSString *currentButtonText = self.titleLabel.text;
objc_setAssociatedObject(self, &ButtonTextObjectKey, currentButtonText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &IndicatorViewKey, indicator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
self.enabled = NO;
[self setTitle:@"" forState:UIControlStateNormal];
[self addSubview:indicator];
}
- (void)hideIndicator
{
NSString *currentButtonText = (NSString *)objc_getAssociatedObject(self, &ButtonTextObjectKey);
UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)objc_getAssociatedObject(self, &IndicatorViewKey);
self.enabled = YES;
[indicator removeFromSuperview];
[self setTitle:currentButtonText forState:UIControlStateNormal];
}
@end