提交信息等待網(wǎng)絡(luò)請求完成或者某一個條件允許的情況下帆谍,我們會在屏幕上或者按鈕上面添加一個菊花体捏,表示當前正處于等待狀態(tài)阻塑,下面看看效果圖巷蚪,以下效果圖是結(jié)合上一篇倒計時方法寫的初狰,具體的實現(xiàn)三椿,待會兒一起看看代碼:
菊花按鈕.gif
調(diào)用代碼:
#pragma mark ---開始倒計時點擊事件
-(void)startAction{
NSInteger time =5;
[_endBt setTitle:[NSString stringWithFormat:@"%zd",time] forState:UIControlStateNormal];
[_startBt showIndicator];//開始菊花轉(zhuǎn)起來
[_startBt startTime:time waitBlock:^(NSInteger remainTime) {
DLog(@"%zd",remainTime);
[_endBt setTitle:[NSString stringWithFormat:@"%zd",remainTime] forState:UIControlStateNormal];
} finishBlock:^{
[_startBt hideIndicator];//菊花消失
DLog(@"finishBlock");
[_endBt setTitle:@"倒計時結(jié)束" 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