按鈕中間帶有菊花的提交按鈕

提交信息等待網絡請求完成或者某一個條件允許的情況下,我們會在屏幕上或者按鈕上面添加一個菊花,表示當前正處于等待狀態,下面看看效果圖,以下效果圖是結合上一篇倒計時方法寫的,具體的實現,待會兒一起看看代碼:

菊花按鈕.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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,829評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,180評論 4 61
  • 起因,爬蟲群有人詢問 requests 抓取結果打印是亂碼怎么解決,于是有其他人指點按照官方的文檔,用r.enco...
    超net閱讀 9,188評論 0 2
  • 早上七點半鬧鐘響起,我皺眉閉眼伸手去把床頭柜上的鬧鐘關掉。昨晚睡前看電視劇看多了,我決定再瞇一會,意識模糊的又呼呼...
    星光社的戴銘閱讀 1,005評論 1 1
  • 時間滴滴答答流逝的太快,生日過了兩輪,日子仿佛直接從元旦跳到了六一。六月中旬的北京極熱,一點也不是我印象當中的北方...
    Jameskof閱讀 250評論 0 0