按钮中间带有菊花的提交按钮

提交信息等待网络请求完成或者某一个条件允许的情况下,我们会在屏幕上或者按钮上面添加一个菊花,表示当前正处于等待状态,下面看看效果图,以下效果图是结合上一篇倒计时方法写的,具体的实现,待会儿一起看看代码:

菊花按钮.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阅读 174,134评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,273评论 4 61
  • 起因,爬虫群有人询问 requests 抓取结果打印是乱码怎么解决,于是有其他人指点按照官方的文档,用r.enco...
    超net阅读 9,210评论 0 2
  • 早上七点半闹钟响起,我皱眉闭眼伸手去把床头柜上的闹钟关掉。昨晚睡前看电视剧看多了,我决定再眯一会,意识模糊的又呼呼...
    星光社的戴铭阅读 1,021评论 1 1
  • 时间滴滴答答流逝的太快,生日过了两轮,日子仿佛直接从元旦跳到了六一。六月中旬的北京极热,一点也不是我印象当中的北方...
    Jameskof阅读 257评论 0 0