如何为 ios 中的每一天设置倒计时?
How to set countdownTimer for each day in ios?
我必须检查设备的当前时间,并在每天凌晨 12 点 day.And 重置标签时间中设置剩余时间的倒数计时器(计时器重新启动为 23:59)。
- (void)updateCountdown {
NSDate *tomorrow = [[NSUserDefaults standardUserDefaults] valueForKey:@"Date"];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *date = [tomorrow dateByAddingTimeInterval:secondsPerDay];
NSDate *startingDate = [NSDate date];
NSDate *endingDate = date;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];
NSInteger hours = [dateComponents hour];
NSInteger minutes = [dateComponents minute];
NSInteger seconds = [dateComponents second];
NSString *countdownText = [NSString stringWithFormat:@" %ld Hours %ld Minutes %ld Seconds", (long)hours, (long)minutes, (long)seconds];
lblRemainigText.text = countdownText;
[self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}
提前致谢。
如果是 00:00,为什么不直接使用计时器检查时间呢?我希望它能有所帮助,我是 Obj-C
的新手
//at viewdidload
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(checkTime)
userInfo:nil
repeats:YES];
-(void)checkTime
{
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:[NSDate date]]; // Get necessary date components
NSInteger dateinSec = [components hour]*60 + [components minute];
NSInteger dayEnd = 24*60-1;
if (dateinSec == dayEnd) {
[self resetSomething];//update when it reach 23:59:59
}
//update ur label with this
NSLog(@"%ld h, %ld m, %ld s till restart", 24-[components hour], 60 - [components minute], 60 - [components second]);
}
我必须检查设备的当前时间,并在每天凌晨 12 点 day.And 重置标签时间中设置剩余时间的倒数计时器(计时器重新启动为 23:59)。
- (void)updateCountdown {
NSDate *tomorrow = [[NSUserDefaults standardUserDefaults] valueForKey:@"Date"];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *date = [tomorrow dateByAddingTimeInterval:secondsPerDay];
NSDate *startingDate = [NSDate date];
NSDate *endingDate = date;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];
NSInteger hours = [dateComponents hour];
NSInteger minutes = [dateComponents minute];
NSInteger seconds = [dateComponents second];
NSString *countdownText = [NSString stringWithFormat:@" %ld Hours %ld Minutes %ld Seconds", (long)hours, (long)minutes, (long)seconds];
lblRemainigText.text = countdownText;
[self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}
提前致谢。
如果是 00:00,为什么不直接使用计时器检查时间呢?我希望它能有所帮助,我是 Obj-C
的新手//at viewdidload
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(checkTime)
userInfo:nil
repeats:YES];
-(void)checkTime
{
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:[NSDate date]]; // Get necessary date components
NSInteger dateinSec = [components hour]*60 + [components minute];
NSInteger dayEnd = 24*60-1;
if (dateinSec == dayEnd) {
[self resetSomething];//update when it reach 23:59:59
}
//update ur label with this
NSLog(@"%ld h, %ld m, %ld s till restart", 24-[components hour], 60 - [components minute], 60 - [components second]);
}