iOS --倒计时
2015-10-12 13:33
337 查看
#import "ViewController.h"
@interface
ViewController (){
IBOutlet UIButton *l_timeButton;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
[l_timeButton
addTarget:self
action:@selector(startTime)
forControlEvents:UIControlEventTouchUpInside];
}
-(void)startTime{
__block int timeout=5;
//倒计时时间
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(), ^{
//设置界面的按钮显示
根据自己需求设置
[l_timeButton
setTitle:@"发送验证码"
forState:UIControlStateNormal];
l_timeButton.userInteractionEnabled =
YES;
});
}else{
// int minutes = timeout / 60;
int seconds = timeout %
60;
NSString *strTime = [NSString
stringWithFormat:@"%.2d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示
根据自己需求设置
NSLog(@"____%@",strTime);
[l_timeButton
setTitle:[NSString
stringWithFormat:@"%@秒后重新发送",strTime]
forState:UIControlStateNormal];
l_timeButton.userInteractionEnabled =
NO;
});
timeout--;
}
});
dispatch_resume(_timer);
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@interface
ViewController (){
IBOutlet UIButton *l_timeButton;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
[l_timeButton
addTarget:self
action:@selector(startTime)
forControlEvents:UIControlEventTouchUpInside];
}
-(void)startTime{
__block int timeout=5;
//倒计时时间
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(), ^{
//设置界面的按钮显示
根据自己需求设置
[l_timeButton
setTitle:@"发送验证码"
forState:UIControlStateNormal];
l_timeButton.userInteractionEnabled =
YES;
});
}else{
// int minutes = timeout / 60;
int seconds = timeout %
60;
NSString *strTime = [NSString
stringWithFormat:@"%.2d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示
根据自己需求设置
NSLog(@"____%@",strTime);
[l_timeButton
setTitle:[NSString
stringWithFormat:@"%@秒后重新发送",strTime]
forState:UIControlStateNormal];
l_timeButton.userInteractionEnabled =
NO;
});
timeout--;
}
});
dispatch_resume(_timer);
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- ios7自定义返回按钮后,右滑返回功能失效解决方法
- iOS获取设备类型
- IOS开发中的几种设计模式介绍
- ios基础学习
- iOS ---属性传值--正向界面传值
- Xcode7开发下 MJRefresh (2.4.12)版本下的使用 问题
- iOS ---利用文本框的协议判断是否输入内容
- iOS ---监听文本框输入
- IOS 页面传值的方法
- IOS使用XMPP最新教程(五)添加好友和接受好友请求
- ios归档
- iOS---TextField知多少
- ios项目重命名
- iOS iOS7越狱
- iOS 处理服务器返回的浮点数数据
- iOS9适配必看指南
- iOS-Git使用详解
- iOS遇到的那些事(持续总结并更新)-MickyChiang
- iOS发布证书申请
- iOS定时器