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

首頁 > 系統 > iOS > 正文

IOS實現驗證碼倒計時功能(二)

2019-10-21 18:54:42
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了IOS實現驗證碼倒計時功能,點擊獲取驗證碼,進入時間倒計時,感興趣的小伙伴們可以參考一下
 

驗證碼倒計時按鈕的應用是非常普遍的,該Blog就和你一起來實現驗證碼倒計時的效果,定義一個發送驗證碼的按鈕,添加點擊事件,具體內容如下

IOS,驗證碼,倒計時

IOS,驗證碼,倒計時

具體代碼:

定義一個發送驗證碼的按鈕,添加點擊事件

 //發送驗證碼按鈕  _sentCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 27 - 4 - 94, CGRectGetMinY(_registerCodeFD.frame) + 4, 94, 40)];  [_sentCodeBtn setBackgroundColor:colorWithRGBA(0, 191, 191, 0.9)];  [_sentCodeBtn setTitle:@"發送驗證碼" forState:UIControlStateNormal];  [_sentCodeBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];  //設置圓角  [_sentCodeBtn.layer setCornerRadius:3.0f];  [_sentCodeBtn.layer setShouldRasterize:YES];  [_sentCodeBtn.layer setRasterizationScale:[UIScreen mainScreen].scale];  //發送事件  [_sentCodeBtn addTarget:self action:@selector(sentCodeMethod) forControlEvents:UIControlEventTouchUpInside];  [self.view addSubview:_sentCodeBtn];

監聽事件:

//發送驗證碼-(void)sentCodeMethod{ NSLog(@"發送驗證碼。。"); //計時器發送驗證碼 [self sentPhoneCodeTimeMethod]; //調用發送驗證碼接口-》}//計時器發送驗證碼-(void)sentPhoneCodeTimeMethod{ //倒計時時間 - 60秒 __block NSInteger timeOut = 59; //執行隊列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //計時器 -》dispatch_source_set_timer自動生成 dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{  if (timeOut <= 0) {   dispatch_source_cancel(timer);   //主線程設置按鈕樣式-》   dispatch_async(dispatch_get_main_queue(), ^{    [_sentCodeBtn setTitle:@"發送驗證碼" forState:UIControlStateNormal];    [_sentCodeBtn setUserInteractionEnabled:YES];   });  }else{   //開始計時   //剩余秒數 seconds   NSInteger seconds = timeOut % 60;   NSString *strTime = [NSString stringWithFormat:@"%.1ld",seconds];   //主線程設置按鈕樣式   dispatch_async(dispatch_get_main_queue(), ^{    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:1.0];    [_sentCodeBtn setTitle:[NSString stringWithFormat:@"%@S后重新發送",strTime] forState:UIControlStateNormal];    [UIView commitAnimations];    //計時器件不允許點擊    [_sentCodeBtn setUserInteractionEnabled:NO];   });   timeOut--;  } }); dispatch_resume(timer);}

以上就是本文的全部內容,希望對大家的學習有所幫助。



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文化| 浪卡子县| 色达县| 光泽县| 沁阳市| 上杭县| 旺苍县| 金门县| 抚州市| 南京市| 固原市| 沙雅县| 晋宁县| 安国市| 同德县| 甘孜县| 枣庄市| 武邑县| 临沭县| 略阳县| 海阳市| 扶绥县| 炎陵县| 青海省| 鄂伦春自治旗| 诏安县| 景洪市| 遂川县| 乌拉特后旗| 永安市| 福鼎市| 濉溪县| 姜堰市| 尚志市| 吉隆县| 靖边县| 甘泉县| 古浪县| 武强县| 扎囊县| 永川市|