美文网首页
iOS-UIButton倒计时

iOS-UIButton倒计时

作者: 安处幽篁兮 | 来源:发表于2017-08-16 16:09 被阅读0次

一般倒计时的使用场景就两种:

  • 发送短信验证码倒计时
  • 广告页倒计时

一、发送短信验证码倒计时

这种情况下,正在倒计时的按钮是不可点击的,也就是说是不可交互的。

二、广告页倒计时

广告页一般都会有个跳过,那么这个倒计时按钮是可点击可交互的。

在倒计时结束时,我们还想让其进入首页


于是乎,就有了下面的分类

@implementation UIButton (CountDown)

/**
 倒计时

 @param timeLine 倒计时时间
 @param title 正常时显示文字
 @param subTitle 倒计时时显示的文字(不包含秒)
 @param countDoneBlock 倒计时结束时的Block
 @param isInteraction 是否希望倒计时时可交互
 */

- (void)countDownWithTime:(NSInteger)timeLine withTitle:(NSString *)title andCountDownTitle:(NSString *)subTitle countDoneBlock:(CountDoneBlock)countDoneBlock isInteraction:(BOOL)isInteraction{
    __block NSInteger timeout = timeLine; // 倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); // 每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束,关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (countDoneBlock) {
                    countDoneBlock(self);
                }
                //设置界面的按钮显示 根据自己需求设置
                self.userInteractionEnabled = YES;
                [self setTitle:title forState:UIControlStateNormal];
                [self setTitleColor:[AXUtils colorWithString:kColor16_Theme] forState:UIControlStateNormal];
                self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
            });
            
        }else{
            
            //            int minutes = timeout / 60;
            
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];;
            if (seconds < 10) {
                strTime = [NSString stringWithFormat:@"%.1d", seconds];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
//                NSLog(@"____%@",strTime);
                
                [self setTitle:[NSString stringWithFormat:@"%@s%@",strTime,subTitle] forState:UIControlStateNormal];
                [self setTitleColor:[AXUtils colorWithString:@"c1c1c1"] forState:UIControlStateNormal];
                self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
                
                self.userInteractionEnabled = isInteraction;
                
            });
            
            timeout--;
            
        }
        
    });
    
    dispatch_resume(_timer);
}

@end

实现效果:

countDown.gif

相关文章

  • iOS-UIButton倒计时

    一般倒计时的使用场景就两种:发送短信验证码倒计时广告页倒计时 一、发送短信验证码倒计时 这种情况下,正在倒计时的按...

  • iOS - 基本控件

    1,iOS之UILabel详解计算lable的size 2,iOS-UIButton 全面解析 3,iOS-UII...

  • iOS-UIButton

    继承关系: inherits from: UIControl : UIView : UIResponder : N...

  • IOS-UIButton使用

    一、IOS UIButton的基本属性 上面就是 UIButton 基本属性的使用。 二、自定义带图片的 UIBu...

  • iOS-UIButton 全面解析

    字数1443 阅读548 评论5 喜欢36UIButton 的全面解析建议收藏,用到的时候来这里一查就都明白了 初...

  • iOS-UIButton 全面解析

    UIButton 的全面解析 如果有错误的地方请大家多多指正,欢迎评论交流,共同学习。

  • iOS-UIButton核心属性

    UIBtton-自定制按钮 在系统的按钮中,我门可以给按钮添加背景图片,文字的属性,但在现实中我们会见到很多上面是...

  • iOS-UIButton的titleEdgeInsets和ima

    UIButton控件上自带了一个UILabel和一个UIImageView的子控件,默认情况下图片居左,文字居右,...

  • iOS-UIButton设置图片文字位置

    当我们想设置一个Button的图片和文字的位置,是不是首想想到利用titleEdgeInsets和imageEdg...

  • 倒计时

    新年倒计时…中考倒计时…高考倒计时…告别学生倒计时…假期倒计时…睡觉倒计时…起床倒计时… 生活中,很多与我们息息相...

网友评论

      本文标题:iOS-UIButton倒计时

      本文链接:https://www.haomeiwen.com/subject/vziirxtx.html