关于倒计时
2013-10-30 15:28
197 查看
1,设定一个时间,当前时间与设定时间进行比较,1秒执行一次;
2,设定起始时间和结束时间,把当前时间和起始时间比较,当当前时间大于等于起始时间开始1步骤
self.timer = [NSTimer
timerWithTimeInterval:1
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:YES];
[[NSRunLoop
mainRunLoop] addTimer:self.timer
forMode:NSDefaultRunLoopMode];
- (void)timerFireMethod:(NSTimer*)theTimer {
if (self.startTime &&
self.endTime) {
NSDateFormatter *dateFormatter = [[NSDateFormatter
alloc] init];
dateFormatter.dateFormat =
@"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone
timeZoneWithAbbreviation:@"GMT"]];
NSDate *startDate = [dateFormatter
dateFromString:self.startTime];
NSDate *endDate = [dateFormatter
dateFromString:self.endTime];
if ([startDate compare:[self
getNowDateFromatAnDate]] ==
NSOrderedAscending &&
[endDate
compare:[self
getNowDateFromatAnDate]] ==
NSOrderedDescending) {
NSArray *date = [self.endTime
componentsSeparatedByString:@" "];
if ([date count] >
1) {
NSCalendar *cal = [NSCalendar
currentCalendar];
NSDateComponents *time = [[NSDateComponents
alloc] init];
NSArray *yearArr = [[date
objectAtIndex:0]
componentsSeparatedByString:@"-"];
if ([yearArr count] ==
3) {
[time
setYear:[[yearArr
objectAtIndex:0]intValue]];
[time
setMonth:[[yearArr
objectAtIndex:1]intValue]];
[time
setDay:[[yearArr objectAtIndex:2]intValue]];
}
NSArray *timeArr = [[date
objectAtIndex:1]
componentsSeparatedByString:@":"];
if ([timeArr count] ==
3) {
[time
setHour:[[timeArr
objectAtIndex:0]intValue]];
[time
setMinute:[[timeArr
objectAtIndex:1]intValue]];
[time
setSecond:[[timeArr
objectAtIndex:2]intValue]];
}
NSDate *todate = [cal
dateFromComponents:time];
NSDate *today = [NSDate
date];
unsigned
int unitFlags =
NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSHourCalendarUnit |
NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *d = [cal
components:unitFlags fromDate:today
toDate:todate options:0];
int hour =[d hour];
int minute = [d minute];
int second = [d second];
if (hour<=0 && minute<=0 && second<=0) {
self.timeLab.text =
@"施工完成";
[self
stop];
}else {
NSString *h =[NSString
stringWithFormat:@"%d",[d
hour]];
NSString *m =[NSString
stringWithFormat:@"%d",[d
minute]];
NSString *s =[NSString
stringWithFormat:@"%d",[d
second]];
self.timeLab.text=
[NSString
stringWithFormat:@"%@:%@:%@",h.length!=1?h:[NSString
stringWithFormat:@"0%@",h],m.length!=1?m:[NSString
stringWithFormat:@"0%@",m],s.length!=1?s:[NSString
stringWithFormat:@"0%@",s]];
}
}
}
if ([endDate compare:[self
getNowDateFromatAnDate]] ==
NSOrderedAscending) {
self.timeLab.text =
@"施工完成";
[self
stop];
}
if ([startDate compare:[self
getNowDateFromatAnDate]] ==
NSOrderedDescending) {
self.timeLab.text =
@"等待施工";
}
}
}
- (void)stop
{
self.timeLab =
nil;
[self.timer
invalidate];
self.timer =
nil;
}
- (NSDate *)getNowDateFromatAnDate
{
NSDate *anyDate = [NSDate
date];
//设置源日期时区
NSTimeZone* sourceTimeZone = [NSTimeZone
timeZoneWithAbbreviation:@"GMT"];
//设置转换后的目标日期时区
NSTimeZone* destinationTimeZone = [NSTimeZone
localTimeZone];
//得到源日期与世界标准时间的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone
secondsFromGMTForDate:anyDate];
//目标日期与本地时区的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone
secondsFromGMTForDate:anyDate];
//得到时间偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//转为现在时间
NSDate* destinationDateNow = [[NSDate
alloc] initWithTimeInterval:interval
sinceDate:anyDate];
return destinationDateNow;
}
2,设定起始时间和结束时间,把当前时间和起始时间比较,当当前时间大于等于起始时间开始1步骤
self.timer = [NSTimer
timerWithTimeInterval:1
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:YES];
[[NSRunLoop
mainRunLoop] addTimer:self.timer
forMode:NSDefaultRunLoopMode];
- (void)timerFireMethod:(NSTimer*)theTimer {
if (self.startTime &&
self.endTime) {
NSDateFormatter *dateFormatter = [[NSDateFormatter
alloc] init];
dateFormatter.dateFormat =
@"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone
timeZoneWithAbbreviation:@"GMT"]];
NSDate *startDate = [dateFormatter
dateFromString:self.startTime];
NSDate *endDate = [dateFormatter
dateFromString:self.endTime];
if ([startDate compare:[self
getNowDateFromatAnDate]] ==
NSOrderedAscending &&
[endDate
compare:[self
getNowDateFromatAnDate]] ==
NSOrderedDescending) {
NSArray *date = [self.endTime
componentsSeparatedByString:@" "];
if ([date count] >
1) {
NSCalendar *cal = [NSCalendar
currentCalendar];
NSDateComponents *time = [[NSDateComponents
alloc] init];
NSArray *yearArr = [[date
objectAtIndex:0]
componentsSeparatedByString:@"-"];
if ([yearArr count] ==
3) {
[time
setYear:[[yearArr
objectAtIndex:0]intValue]];
[time
setMonth:[[yearArr
objectAtIndex:1]intValue]];
[time
setDay:[[yearArr objectAtIndex:2]intValue]];
}
NSArray *timeArr = [[date
objectAtIndex:1]
componentsSeparatedByString:@":"];
if ([timeArr count] ==
3) {
[time
setHour:[[timeArr
objectAtIndex:0]intValue]];
[time
setMinute:[[timeArr
objectAtIndex:1]intValue]];
[time
setSecond:[[timeArr
objectAtIndex:2]intValue]];
}
NSDate *todate = [cal
dateFromComponents:time];
NSDate *today = [NSDate
date];
unsigned
int unitFlags =
NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSHourCalendarUnit |
NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *d = [cal
components:unitFlags fromDate:today
toDate:todate options:0];
int hour =[d hour];
int minute = [d minute];
int second = [d second];
if (hour<=0 && minute<=0 && second<=0) {
self.timeLab.text =
@"施工完成";
[self
stop];
}else {
NSString *h =[NSString
stringWithFormat:@"%d",[d
hour]];
NSString *m =[NSString
stringWithFormat:@"%d",[d
minute]];
NSString *s =[NSString
stringWithFormat:@"%d",[d
second]];
self.timeLab.text=
[NSString
stringWithFormat:@"%@:%@:%@",h.length!=1?h:[NSString
stringWithFormat:@"0%@",h],m.length!=1?m:[NSString
stringWithFormat:@"0%@",m],s.length!=1?s:[NSString
stringWithFormat:@"0%@",s]];
}
}
}
if ([endDate compare:[self
getNowDateFromatAnDate]] ==
NSOrderedAscending) {
self.timeLab.text =
@"施工完成";
[self
stop];
}
if ([startDate compare:[self
getNowDateFromatAnDate]] ==
NSOrderedDescending) {
self.timeLab.text =
@"等待施工";
}
}
}
- (void)stop
{
self.timeLab =
nil;
[self.timer
invalidate];
self.timer =
nil;
}
- (NSDate *)getNowDateFromatAnDate
{
NSDate *anyDate = [NSDate
date];
//设置源日期时区
NSTimeZone* sourceTimeZone = [NSTimeZone
timeZoneWithAbbreviation:@"GMT"];
//设置转换后的目标日期时区
NSTimeZone* destinationTimeZone = [NSTimeZone
localTimeZone];
//得到源日期与世界标准时间的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone
secondsFromGMTForDate:anyDate];
//目标日期与本地时区的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone
secondsFromGMTForDate:anyDate];
//得到时间偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//转为现在时间
NSDate* destinationDateNow = [[NSDate
alloc] initWithTimeInterval:interval
sinceDate:anyDate];
return destinationDateNow;
}
相关文章推荐
- 关于倒计时的显示问题
- 关于如何在自己的叶子上添加KDE4.2倒计时
- Android 关于倒计时功能的说说
- 关于倒计时setInterval()方法与setTimeout()小结整合
- 蓝桥杯倒计时1——关于代码填空问题
- 关于listview中添加倒计时功能卡顿问题
- Android 关于倒计时功能的实现
- 【微信支付】分享一个失败的案例 跨域405(Method Not Allowed)问题 关于IM的一些思考与实践 基于WebSocketSharp 的IM 简单实现 【css3】旋转倒计时 【Html5】-- 塔台管制 H5情景意识 --飞机 谈谈转行
- android 关于倒计时CountDownTimer的用法和实例
- iOS关于在UITableView中,实现多个cell中不同的倒计时实现
- 关于倒计时的小demo学习
- 关于数字游戏中的倒计时计算方式
- 一个关于倒计时的类
- 关于倒计时的设置
- 关于android ListView倒计时遇到的一些问题
- 关于商场倒计时活动!
- javascript 一个关于时间排序的算法(一个页面多个倒计时排序)
- 关于js倒计时的模板 可在多个项目中复用
- 关于人生倒计时的一个小玩意,纯属业余
- 关于倒计时new Date().getTime()出现NaN