用nstimer实现倒计时
用nstimer实现倒计时
//
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES]; //
- (void)timerFireMethod:(NSTimer *)theTimer
{
BOOL timeStart = YES;
NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
NSDateComponents *endTime = [[NSDateComponents alloc] init]; //初始化目标时间...
NSDate *today = [NSDate date]; //得到当前时间 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateString = [dateFormatter dateFromString:todate];
NSString *overdate = [dateFormatter stringFromDate:dateString];
// NSLog(@"overdate=%@",overdate);
static int year;
static int month;
static int day;
static int hour;
static int minute;
static int second;
if(timeStart) {//从NSDate中取出年月日,时分秒,但是只能取一次
year = [[overdate substringWithRange:NSMakeRange(, )] intValue];
month = [[overdate substringWithRange:NSMakeRange(, )] intValue];
day = [[overdate substringWithRange:NSMakeRange(, )] intValue];
hour = [[overdate substringWithRange:NSMakeRange(, )] intValue];
minute = [[overdate substringWithRange:NSMakeRange(, )] intValue];
second = [[overdate substringWithRange:NSMakeRange(, )] intValue];
timeStart= NO;
} [endTime setYear:year];
[endTime setMonth:month];
[endTime setDay:day];
[endTime setHour:hour];
[endTime setMinute:minute];
[endTime setSecond:second];
NSDate *overTime = [cal dateFromComponents:endTime]; //把目标时间装载入date
//用来得到具体的时差,是为了统一成北京时间
unsigned int unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit| NSMinuteCalendarUnit| NSSecondCalendarUnit;
NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:overTime options:];
NSString *t = [NSString stringWithFormat:@"%d", [d day]];
NSString *h = [NSString stringWithFormat:@"%d", [d hour]];
NSString *fen = [NSString stringWithFormat:@"%d", [d minute]];
if([d minute] < ) {
fen = [NSString stringWithFormat:@"0%d",[d minute]];
}
NSString *miao = [NSString stringWithFormat:@"%d", [d second]];
if([d second] < ) {
miao = [NSString stringWithFormat:@"0%d",[d second]];
}
// NSLog(@"===%@天 %@:%@:%@",t,h,fen,miao);
[_longtime setText:[NSString stringWithFormat:@"%@天 %@:%@:%@",t,h,fen,miao]];
if([d second] > ) {
//计时尚未结束,do_something
// [_longtime setText:[NSString stringWithFormat:@"%@:%@:%@",d,fen,miao]];
} else if([d second] == ) {
//计时结束 do_something } else{
//计时器失效
[theTimer invalidate];
} }
用nstimer实现倒计时的更多相关文章
- 使用NSTimer实现倒计时-备
今天在CocoaChina上面看到有人在问倒计时怎么做,记得以前在看Iphone31天的时候做过一个,今天翻出来运行不了了,原因是我的IphoneSDK升级到3.1了,以前使用的是2.2.1,在2.2 ...
- 倒计时实现两种方法-NSTimer/GCD
#import "ViewController.h" @interface ViewController () @property (nonatomic ,strong)UIBut ...
- IOS中定时器NSTimer的开启与关闭
调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...
- swift - 利用UIDatePicker实现定时器的效果
效果图如下: 可以通过UIDatePicker调整倒计时的时间,然后点击UIButton开始倒计时,使用NSTimer进行倒计时的时间展示,我是声明了一个label也进行了标记, 然后点击按钮开始倒计 ...
- NSTimer+倒计时功能实现
NSTimer 一.前言,查看官方文档,可以发现NSTimer是Foundation框架下的一个类,它直接继承与NSObject. 二.常用属性 1. @property (copy) NSDate ...
- iOS 倒计时NSTimer
项目中可能会遇到有些倒计时的地方 比方 手机验证的时候,验证码一般都会有一个时间限制,此时在输入验证码的地方就须要展示一个倒计时 详细实现方式是使用了iOS 自带的 NSTimer 上代码 首先新建 ...
- NSTimer实现读秒、倒计时等周期性操作
self.timerSchedule = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(spin ...
- iOS 商品倒计时 限时特价 限时优惠 功能的封装
最近项目中多个页面用到了 商品特价倒计时的功能 为了偷懒 于是自己封装了一个限时抢购 倒计时的view 代码实现如下: 定向价 限时特价 模型代码实现: #pragma mark 商品定向价模型 @ ...
- ios 简单的倒计时验证码数秒过程实现
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) ...
随机推荐
- [深入浅出WP8.1(Runtime)]浮出控件(Flyout)
4.15 浮出控件(Flyout) 浮出控件(Flyout)是一个轻型的辅助型的弹出控件,通常会作为提示或者要求用户进行相关的交互来使用.Flyout控件与Windows Phone里面的弹出框Mes ...
- 【BZOJ1008】1008: [HNOI2008]越狱 简单组合数学+快速幂
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...
- PHP面向对象学习七 总结
1.对象描述的配置 方法名 __tostring() 我们可以直接打印对象句柄,从而获得该方法的基本信息或其他内容. class My{ function __tostring ( ){ echo & ...
- 优化UITableViewCell高度计算的那些事
优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...
- Java_DES 加密和解密源码
Java密码学结构设计遵循两个原则: 1) 算法的独立性和可靠性. 2) 实现的独立性和相互作用性. 算法的独立性是通过定义密码服务类来获得.用户只需了解密码算法的概念,而不用去关心如何实现这些概念. ...
- 基于Solr的HBase多条件查询测试
背景: 某电信项目中采用HBase来存储用户终端明细数据,供前台页面即时查询.HBase无可置疑拥有其优势,但其本身只对rowkey支持毫秒级 的快 速检索,对于多字段的组合查询却无能为力.针对HBa ...
- C#常用方法二
public sealed class StringTool { /// <summary> /// 将txt文件读入字符串 /// </summary> /// <pa ...
- 复制 VS 复用 -04
(续上篇) 小菜:“我明白了,他说用任意一种面向对象语言实现,那意思就是要用面向对象的编程方法去实现,对吗?OK,这个我学过,只不过当时我没想到而已.” 大鸟:“所有编程初学者都会有这 ...
- linux下的crontab服务
linux下的crontab服务:1.crontab 是用来让使用者在固定时间或固定间隔执行程序之用在linux平台上如果需要实现任务调度功能可以编写cron脚本来实现.以某一频率执行任务linux缺 ...
- Find a way——L
L. Find a way Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave N ...