用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:) ...
随机推荐
- 如何在 ejs 模板中使用 helper function 外部函数进行特殊处理?
一般我们想要在 ejs 模板中使用外部函数用于特殊的处理,比如:<%= ellipsis(title, 30) %> 通常的做法是: 使用 app.locals 来定义: app.loca ...
- ROW_NUMBER() OVER函数的基本用法
转自:http://www.cnblogs.com/icebutterfly/archive/2009/08/05/1539657.html 语法:ROW_NUMBER() OVER(PARTITIO ...
- jQuery AJAX实例
<html><head><title>jQuery Ajax 实例演示</title></head><script language= ...
- HDU-Minimum Inversion Number(最小逆序数)
Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...
- 【bzoj2179】FFT快速傅立叶 FFT模板
2016-06-01 09:34:54 很久很久很久以前写的了... 今天又比较了一下效率,貌似手写复数要快很多. 贴一下模板: #include<iostream> #include& ...
- 51nod百度之星2016练习赛
今天看了看51nod发现有这样一个练习赛,就做了做.因为实力太弱想不出E题,各位神犇勿D. (5.26UPD:E题想粗来了) A 区间交 不难发现若干线段[li,ri]的交就是[max(li),min ...
- windows一些快捷键
1.Win + __ 1)Win + L 锁屏 2)Win + E 资源管理器(就是打开硬盘) 3)Win + D 回到桌面 4)Win + Tab 3D方式切换程序窗口 5)Win + R 运行命令 ...
- 如何将maven项目导入MyEclipse
一.安装maven第一步:下载一个免安装版的apache-maven-3.0.3.zip解压后,配置环境变量 新建M2_HOME: 在path后面添加 %M2_HOME%\bin; 第二步: ...
- MRP运算生成采购单时间的逻辑
由MRP运算产生的采购单日期,由生产单指定的安排计划日期.公司设置里的采购提前期和隐藏的供应商供货提前期三个字段共同决定. 可以很容易的在系统中找到,供应商供货提前期,需要在产品视图中将字段selle ...
- iBatis in 语句参数传入方法
刚刚开始在工作中用到iBatis 在用到in去查询或者删除 我本来是传递一个String的参数,但是总是报以下的错误