iOS如何獲取短信驗(yàn)證碼倒計(jì)時(shí)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)iOS如何獲取短信驗(yàn)證碼倒計(jì)時(shí),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、坡頭網(wǎng)絡(luò)推廣、成都微信小程序、坡頭網(wǎng)絡(luò)營銷、坡頭企業(yè)策劃、坡頭品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供坡頭建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:muchs.cn

方法一:

網(wǎng)上用的很多的一種,不多說,直接上代碼.

-(void)startTime{
  __block int timeout= 60; //倒計(jì)時(shí)時(shí)間
  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); //每秒執(zhí)行
  dispatch_source_set_event_handler(_timer, ^{
    if(timeout<=0){ //倒計(jì)時(shí)結(jié)束,關(guān)閉
      dispatch_source_cancel(_timer);
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.getIdentifyCodeBt setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal];
        self.getIdentifyCodeBt.userInteractionEnabled = YES;
        [self.getIdentifyCodeBt setTitleColor:THEME_RED forState:UIControlStateNormal];
        self.getIdentifyCodeBt.backgroundColor = [UIColor whiteColor];
        self.getIdentifyCodeBt.layer.borderColor = THEME_RED.CGColor;
      });
    }else{
      dispatch_async(dispatch_get_main_queue(), ^{

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [self.getIdentifyCodeBt setTitle:[NSString stringWithFormat:@"%zd秒后失效",timeout] forState:UIControlStateNormal];
        [self.getIdentifyCodeBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.getIdentifyCodeBt.backgroundColor = [UIColor lightGrayColor];
        self.getIdentifyCodeBt.layer.borderColor = [UIColor clearColor].CGColor;
        self.getIdentifyCodeBt.clipsToBounds = YES;
        [UIView commitAnimations];
        self.getIdentifyCodeBt.userInteractionEnabled = NO;
      });
      timeout--;
    }
  });
  dispatch_resume(_timer);

}

到時(shí)直接調(diào)用就可以了。

方法二:利用分類

給UIButton新建一個(gè)分類

.h文件如下

#import <UIKit/UIKit.h>

@interface UIButton (XSCountDown)
- (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration;
- (void)xs_stopCountDown;
@end

.m文件如下

#import "UIButton+XSCountDown.h"

#import "ThemeColor.h"
static NSTimer *_countTimer;
static NSTimeInterval _count;
static NSString *_title;

@implementation UIButton (XSCountDown)

- (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration {
  _title = self.titleLabel.text;
  _count = duration;
  _countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(xs_updateTitle) userInfo:nil repeats:YES];
  [[NSRunLoop mainRunLoop] addTimer:_countTimer forMode:NSRunLoopCommonModes];
  self.userInteractionEnabled = NO;

   [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  self.backgroundColor = [UIColor lightGrayColor];
  self.layer.borderColor = [UIColor clearColor].CGColor;
  self.clipsToBounds = YES;
}

- (void)xs_stopCountDown {
  [_countTimer invalidate];
  _countTimer = nil;
  _count = 60.0;
  [self setTitle:_title forState:UIControlStateNormal];
  self.userInteractionEnabled = YES;
}

- (void)xs_updateTitle {
  NSString *countString = [NSString stringWithFormat:@"%lis 后失效", (long)_count - 1];
  self.userInteractionEnabled = NO;
  [self setTitle:countString forState:UIControlStateNormal];
  if (_count-- <= 1.0) {
    [self xs_stopCountDown];
    [self setTitleColor:THEME_RED forState:UIControlStateNormal];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderColor = THEME_RED.CGColor;
  }

}

@end

然后在controller里直接調(diào)用分類.h文件里的方法就ok了

[self.verifyBt xs_beginCountDownWithDuration:60.0];

關(guān)于“iOS如何獲取短信驗(yàn)證碼倒計(jì)時(shí)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前標(biāo)題:iOS如何獲取短信驗(yàn)證碼倒計(jì)時(shí)-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article48/degdhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、App開發(fā)網(wǎng)站內(nèi)鏈、品牌網(wǎng)站制作營銷型網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管