IOS开发代码分享之用nstimer实现倒计时功能

2016-02-19 11:30 26 1 收藏

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐IOS开发代码分享之用nstimer实现倒计时功能,无聊中的都看过来。

【 tulaoshi.com - 编程语言 】

用nstimer实现倒计时功能,废话不多说,直接上代码,详细解释请参照注释

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)
// [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(0, 4)] intValue];         month = [[overdate substringWithRange:NSMakeRange(5, 2)] intValue];         day = [[overdate substringWithRange:NSMakeRange(8, 2)] intValue];         hour = [[overdate substringWithRange:NSMakeRange(11, 2)] intValue];         minute = [[overdate substringWithRange:NSMakeRange(14, 2)] intValue];         second = [[overdate substringWithRange:NSMakeRange(17, 2)] 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:0];     NSString *t = [NSString stringWithFormat:@"%d", [d day]];     NSString *h = [NSString stringWithFormat:@"%d", [d hour]];     NSString *fen = [NSString stringWithFormat:@"%d", [d minute]];     if([d minute]  10) {         fen = [NSString stringWithFormat:@"0%d",[d minute]];     }     NSString *miao = [NSString stringWithFormat:@"%d", [d second]];     if([d second]  10) {         miao = [NSString stringWithFormat:@"0%d",[d second]];     } //    NSLog(@"===%@天 %@:%@:%@",t,h,fen,miao);     [_longtime setText:[NSString stringWithFormat:@"%@天 %@:%@:%@",t,h,fen,miao]];     if([d second]  0) {         //计时尚未结束,do_something //        [_longtime setText:[NSString stringWithFormat:@"%@:%@:%@",d,fen,miao]];     } else if([d second] == 0) {         //计时结束 do_something              } else{ //计时器失效        [theTimer invalidate];     }      }

来源:https://www.tulaoshi.com/n/20160219/1597709.html

延伸阅读
借助Authorware提供的时间变量和时间函数,我们可以方便地制作倒计时牌。本文中的例子是具有倒计时功能的贺卡。 Authorware 4.0中的时间变量和时间函数很多,在这个例子中需要用到以下几个: hour(当前的小时数),minute(当前的分钟数),sec(当前的秒数);datetonum(day, month,year),此函数返回一个公元1900年1月1日到参数...
利用JavaScript制作倒计时牌 五一劳动节很快就到了。哈哈,到时可以出去好好玩一玩了。是不是在掐指计算了。下面教你一种方法在网页上面制作倒计时牌,这样就方便多了。 把下面的JavaScript代码加入到< body < /body 中即可。 < script language="JavaScript" var urodz= new Date("5/1/2005"); var s="今天离五一劳动...
潮自拍怎么开启倒计时拍照   打开潮自拍进入应用后点击中间图标。进入拍照页面后点击上方中间图标,打开功能键后点击最右边图标,根据需要设置秒数。此时点击拍照键后便会开始倒计时,倒计时完成后便将自动拍照。
实操利用PowerPoint制作倒计时   1、新建一个空白板式的ppt,选择合适的模板,输入题目艺术字倒计时和时间到和相应的文字时间还剩秒,调节字体和大小位置。在利用自选图形里的圆角矩形拖出一个圆角矩形,高2.2,宽19.41,内部填充效果如下,再复制出一个相同的,内部无颜色填充。再添加一个按钮,添加文字为开始计时。 2、选...
标签: 怀孕
孕前全面身体检查 血常规(血型) 及早发现贫血等血液体统疾病,因为如果母亲贫血,不仅会出现产后tulaoshi.com出血、产褥感染等并发症,还会殃及宝宝,给宝宝带来一系列影响,例如易感染、抵抗力下降、生长发育落后等。 尿常规 有助于肾脏疾患早期的诊断。十个月的孕期对于母亲的肾脏系统是一个巨大的考验,身体的代谢增加,会使肾脏的负担...

经验教程

110

收藏

65
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部