1.通過NSThread的performSelectorInBackground;
2.通過定時器,屬于比較簡單的寫法;
3.通過GCD中的dispatch_source;
先說第一種:
#import "ViewController.h"
@interface ViewController ()
{
int _count;
UILabel *_label;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//多線程讀秒操作(可以加在button里控制)
[self performSelectorInBackground:@selector(thread) withObject:nil];
_label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
_label.backgroundColor=[UIColor orangeColor];
_label.textAlignment=NSTextAlignmentCenter;
_label.font=[UIFont systemFontOfSize:30];
_label.text=@"60";
[self.view addSubview:_label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 在異步線程中無法操作UI,如果想要操作UI必須回調主線程
- (void)thread
{
for(int i=59;i>=0;i--)
{
_count = i;
// 回調主線程
[self performSelectorOnMainThread:@selector(mainThread) withObject:nil waitUntilDone:YES];
sleep(1);
}
}
// 此函數(shù)主線程執(zhí)行
- (void)mainThread
{
_label.text=[NSString stringWithFormat:@"%d",_count];
if (_count==0) {
}
}
@end
第二種:
#import "ViewController.h"
@interface ViewController ()
{
UIButton *btn;
int timeDown; //60秒后重新獲取驗證碼
NSTimer *timer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(10, 200, 300, 50);
btn.backgroundColor=[UIColor cyanColor];
[btn setBackgroundImage:[UIImage imageNamed:@"bg_send_validate_code.png"] forState:UIControlStateNormal];
[btn setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnAction{
timeDown = 59;
[self handleTimer];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
}
-(void)handleTimer
{
if(timeDown>=0)
{
[btn setUserInteractionEnabled:NO];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
int sec = ((timeDown%(24*3600))%3600)%60;
[btn setTitle:[NSString stringWithFormat:@"(%d)重發(fā)驗證碼",sec] forState:UIControlStateNormal];
}
else
{
[btn setUserInteractionEnabled:YES];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setTitle:@"重發(fā)驗證碼" forState:UIControlStateNormal];
[timer invalidate];
}
timeDown = timeDown - 1;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
第三種:
#import "ViewController.h"
@interface ViewController ()
{
UIButton *_getNumBtn;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_getNumBtn=[UIButton buttonWithType:UIButtonTypeCustom];
_getNumBtn.frame=CGRectMake(10, 200, 300, 50);
[_getNumBtn setBackgroundImage:[UIImage imageNamed:@"bg_send_validate_code.png"] forState:UIControlStateNormal];
[_getNumBtn setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
[_getNumBtn addTarget:self action:@selector(getNumBtnAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_getNumBtn];
}
- (void)getNumBtnAction{
__block NSInteger second = 60;
//全局隊列 默認優(yōu)先級
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//定時器模式 事件源
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
//NSEC_PER_SEC是秒,*1是每秒
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), NSEC_PER_SEC * 1, 0);
//設置響應dispatch源事件的block,在dispatch源指定的隊列上運行
dispatch_source_set_event_handler(timer, ^{
//回調主線程,在主線程中操作UI
dispatch_async(dispatch_get_main_queue(), ^{
if (second >= 0) {
[_getNumBtn setTitle:[NSString stringWithFormat:@"(%ld)重發(fā)驗證碼",second] forState:UIControlStateNormal];
second--;
}
else
{
//這句話必須寫否則會出問題
dispatch_source_cancel(timer);
[_getNumBtn setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
}
});
});
//啟動源
dispatch_resume(timer);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end