iOS 인증 코드 보 내기 카운트다운 적용

app 등록 할 때 인증 코드 를 보 내 는 기능 이 자주 발생 합 니 다.인증 코드 를 보 내 려 면 그 button 을 누 르 면 카운트다운 이 시 작 됩 니 다.시간 이 끝나 야 다시 보 낼 수 있 습 니 다.효 과 는 다음 과 같 습 니 다.

구체 적 인 코드 는 다음 과 같다.

- (IBAction)sendMes:(UIButton *)sender 
{ 
 __block int timeout = 10 ; //      
 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, ^{ //          NSTimer          
  if(timeout<=0){ //     ,   
   dispatch_source_cancel(_timer); 
    
   dispatch_async(dispatch_get_main_queue(), ^{ 
    //          
    [sender setTitle:@"     " forState:UIControlStateNormal]; 
    sender.enabled = YES; 
   }); 
    
  }else{ 
   int seconds = timeout % 60; 
   NSString *strTime = [NSString stringWithFormat:@"%d", seconds]; 
   dispatch_async(dispatch_get_main_queue(), ^{ 
    NSLog(@"----%@", [NSThread currentThread]); 
    //          
    [sender setTitle:[NSString stringWithFormat:@"(%@)    ",strTime] forState:UIControlStateDisabled]; 
    sender.enabled = NO; 
   }); 
   timeout--; 
  } 
 }); 
 //     resume          
} 

이 button 은 storyboard 에서 직접 구현 할 수 있 습 니 다.button 의 배경 색 은 회색 으로 선택 하고 배경 그림 을 설정 합 니 다.예 를 들 어 위의 빨간색 그림,button 의 type 은 custom 으로 설정 합 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기