国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS獲取短信驗證碼倒計時的兩種實現方法

2019-10-21 18:46:26
字體:
來源:轉載
供稿:網友

方法一:

網上用的很多的一種,不多說,直接上代碼.

-(void)startTime{  __block int timeout= 60; //倒計時時間  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(), ^{        [self.getIdentifyCodeBt setTitle:@"獲取驗證碼" 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);}

到時直接調用就可以了。

方法二:利用分類

給UIButton新建一個分類

.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里直接調用分類.h文件里的方法就ok了

[self.verifyBt xs_beginCountDownWithDuration:60.0];

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 葫芦岛市| 呼和浩特市| 澄江县| 永丰县| 武功县| 柳林县| 扶沟县| 湟源县| 顺义区| 万年县| 万山特区| 措美县| 安西县| 金溪县| 永胜县| 申扎县| 忻城县| 津市市| 陈巴尔虎旗| 巴中市| 元氏县| 阳山县| 久治县| 海门市| 雷波县| 高尔夫| 太原市| 洛宁县| 平度市| 锦州市| 芷江| 本溪| 梧州市| 灵丘县| 和田市| 山西省| 昌黎县| 伊金霍洛旗| 蓬莱市| 当涂县| 左权县|