如何在倒数计时器中添加年份和月份 Objective C
How to add year and month in count down Timer Objective C
现在我已经添加了秒和分钟。在我的计时器里。此外,我需要在我的计时器中添加 Hours 月和年,如下图所示:
这是我实现的代码:
// CountDown Timer
In My view DidLoad I have initialized my variables:
currHour = 1;
currMinute= 3;
currSeconds= 00;
-(void)start {
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}
-(void)timerFired {
if((currHour>0 || currHour>=0) && (currMinute>0 || currSeconds>=0) && currMinute>=0) {
if(currSeconds==0) {
currMinute-=1;
currSeconds=59;
}
else if(currSeconds>0) {
currSeconds-=1;
}
if(currMinute>-1)
[self.progress setText:[NSString stringWithFormat:@"%d%@%02d %@",currMinute,@":",currSeconds,@"Sec"]];
}
else {
self.countDownView.hidden = TRUE;
[timer invalidate];
}
}
工作到分秒必争。
提前致谢。
添加到 viewDidLoad 下一个代码并将目标日期保存为 属性 或 ivar
NSTimeInterval currentDateSeconds = [[NSDate date] timeIntervalSince1970];
NSTimeInterval targetDateSeconds = currentDateSeconds + [self secondsInDay:0 hour:1 min:3 sec:0];
targetDate = [NSDate dateWithTimeIntervalSince1970:targetDateSeconds];
然后添加将目标日期转换为秒的方法,例如在你的例子中是 1 小时 3 分钟
- (NSTimeInterval) secondsInDay: (int) day hour:(int)hour min:(int)min sec: (int)sec {
NSTimeInterval secondsInOneDay = 24 * 60 * 60;
return day * secondsInOneDay + hour * 60 * 60 + min * 60 + sec; }
然后使用下一个timeFired
方法:
-(void)timerFired {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:[NSDate date] toDate:targetDate options:0];
NSInteger year = dateComponents.year;
NSInteger month = dateComponents.month;
NSInteger day = dateComponents.day;
NSInteger minute = dateComponents.minute;
NSInteger second = dateComponents.second;
if(year == 0 &&
month == 0 &&
day == 0 &&
minute == 0 &&
second == 0) {
NSLog(@"FINISH");
[timer invalidate];
} else {
NSLog(@"%@", [NSString stringWithFormat:@"%d%@%02d %@",minute,@":",second,@"Sec"]);
}}
如果您对块使用 NSTimer
方法,则不需要存储任何时间变量(或与此相关的计时器),这很简洁:
- (void)startCountdownWithNumberOfSeconds:(NSTimeInterval)seconds {
__block NSTimeInterval remainingTime = seconds;
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer *timer) {
NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
fromDate:[NSDate date]
toDate:[NSDate dateWithTimeIntervalSinceNow:remainingTime--]
options:kNilOptions];
NSLog(@"Remaining time: %@", components.description);
if (remainingTime > 0) {
self.progress.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", (int)components.day, (int)components.hour, (int)components.minute, (int)components.second];
}
else {
[timer invalidate];
}
}];
}
NSCalendar
有很多单位选项,例如 NSCalendarUnitYear
和 NSCalendarUnitMonth
,因此您可以在需要时将它们添加到列表中。
现在我已经添加了秒和分钟。在我的计时器里。此外,我需要在我的计时器中添加 Hours 月和年,如下图所示:
这是我实现的代码:
// CountDown Timer
In My view DidLoad I have initialized my variables:
currHour = 1;
currMinute= 3;
currSeconds= 00;
-(void)start {
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}
-(void)timerFired {
if((currHour>0 || currHour>=0) && (currMinute>0 || currSeconds>=0) && currMinute>=0) {
if(currSeconds==0) {
currMinute-=1;
currSeconds=59;
}
else if(currSeconds>0) {
currSeconds-=1;
}
if(currMinute>-1)
[self.progress setText:[NSString stringWithFormat:@"%d%@%02d %@",currMinute,@":",currSeconds,@"Sec"]];
}
else {
self.countDownView.hidden = TRUE;
[timer invalidate];
}
}
工作到分秒必争。
提前致谢。
添加到 viewDidLoad 下一个代码并将目标日期保存为 属性 或 ivar
NSTimeInterval currentDateSeconds = [[NSDate date] timeIntervalSince1970];
NSTimeInterval targetDateSeconds = currentDateSeconds + [self secondsInDay:0 hour:1 min:3 sec:0];
targetDate = [NSDate dateWithTimeIntervalSince1970:targetDateSeconds];
然后添加将目标日期转换为秒的方法,例如在你的例子中是 1 小时 3 分钟
- (NSTimeInterval) secondsInDay: (int) day hour:(int)hour min:(int)min sec: (int)sec {
NSTimeInterval secondsInOneDay = 24 * 60 * 60;
return day * secondsInOneDay + hour * 60 * 60 + min * 60 + sec; }
然后使用下一个timeFired
方法:
-(void)timerFired {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:[NSDate date] toDate:targetDate options:0];
NSInteger year = dateComponents.year;
NSInteger month = dateComponents.month;
NSInteger day = dateComponents.day;
NSInteger minute = dateComponents.minute;
NSInteger second = dateComponents.second;
if(year == 0 &&
month == 0 &&
day == 0 &&
minute == 0 &&
second == 0) {
NSLog(@"FINISH");
[timer invalidate];
} else {
NSLog(@"%@", [NSString stringWithFormat:@"%d%@%02d %@",minute,@":",second,@"Sec"]);
}}
如果您对块使用 NSTimer
方法,则不需要存储任何时间变量(或与此相关的计时器),这很简洁:
- (void)startCountdownWithNumberOfSeconds:(NSTimeInterval)seconds {
__block NSTimeInterval remainingTime = seconds;
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer *timer) {
NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
fromDate:[NSDate date]
toDate:[NSDate dateWithTimeIntervalSinceNow:remainingTime--]
options:kNilOptions];
NSLog(@"Remaining time: %@", components.description);
if (remainingTime > 0) {
self.progress.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", (int)components.day, (int)components.hour, (int)components.minute, (int)components.second];
}
else {
[timer invalidate];
}
}];
}
NSCalendar
有很多单位选项,例如 NSCalendarUnitYear
和 NSCalendarUnitMonth
,因此您可以在需要时将它们添加到列表中。