UIButton添加倒計時

來源:互聯網
上載者:User

標籤:ack   icon   efault   can   res   cond   control   integer   span   

 

最近一個項目有擷取手機簡訊跟郵箱驗證碼功能, 所以要加一個UIButton倒計時功能

例子代碼如下:

 1 - //擷取驗證碼按鈕 2 - - (IBAction)getButtonClick:(UIButton *)sender; 3 - 4 - #pragma mark - 擷取驗證碼 5 - - (IBAction)getButtonClick:(UIButton *)sender 6 - { 7 -     //正常狀態下的背景顏色 8 -     UIColor *mainColor = [UIColorcolorWithRed:84/255.0green:180/255.0blue:98/255.0alpha:1.0f]; 9 -     //倒計時狀態下的顏色10 -     UIColor *countColor = [UIColorlightGrayColor];11 -     [selfsetTheCountdownButton:sender startWithTime:5title:@"擷取驗證碼"countDownTitle:@"s"mainColor:mainColor countColor:countColor];12 - }13 -14 - #pragma mark - button倒計時15 - - (void)setTheCountdownButton:(UIButton *)button startWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subTitle mainColor:(UIColor *)mColor countColor:(UIColor *)color {16 -     //倒計時時間17 -     __block NSInteger timeOut = timeLine;18 -     dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);19 -     dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0, queue);20 -     //每秒執行一次21 -     dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0), 1.0 * NSEC_PER_SEC,0);22 -     dispatch_source_set_event_handler(_timer, ^{23 -24 -         //倒計時結束,關閉25 -         if (timeOut == 0) {26 -             dispatch_source_cancel(_timer);27 -             dispatch_async(dispatch_get_main_queue(), ^{28 -                 button.backgroundColor = mColor;29 -                 [button setTitle:titleforState:UIControlStateNormal];30 -                 button.userInteractionEnabled =YES;31 -             });32 -         } else {33 -             int seconds = timeOut % 60;34 -             NSString *timeStr = [NSStringstringWithFormat:@"%0.1d", seconds];35 -             dispatch_async(dispatch_get_main_queue(), ^{36 -                 button.backgroundColor = color;37 -                 [button setTitle:[NSStringstringWithFormat:@"%@%@",timeStr,subTitle]forState:UIControlStateNormal];38 -                 button.userInteractionEnabled =NO;39 -             });40 -             timeOut--;41 -         }42 -     });43 -     dispatch_resume(_timer);44 - }

 

UIButton添加倒計時

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.