gpt4 book ai didi

objective-c - 倒数日子 - iPhone倒计时

转载 作者:搜寻专家 更新时间:2023-10-30 20:01:52 25 4
gpt4 key购买 nike

我正在尝试制作一个计数器,显示离我们去欧洲旅行还有多少天。它只有大约 70 天(截至今天)所以我认为我不必担心天文数字或任何东西,但我真的很难过 - 我附上了一些 friend 给我的代码,它不也工作。相信我,我已经尝试了我能想到的一切——在任何人咬我的头之前,我在这些论坛上看到过,是的,我确实非常广泛地查看了 Apple 文档,但我不是 100%确定从哪里开始 - 我已经尝试了 NSTimer、NSDate 和它们所有的子类和方法,但没有任何东西可以立即跳出。

就我认为我实际应该做的事情而言,我认为我需要以某种方式为今天/现在/当天的“天”分配一个整数值,它将使用 [NSDate date 动态变化] 然后对于我们离开的日期也是如此。倒计时只是在再次调用该方法时更新(如果需要,我可以使用 NSTimer 执行此操作)并且倒计时上显示的值是这两个值之间的差值。

我并不是特别想要一种在我们离开之前每秒更新一次的闪烁的东西 - 我个人认为这很俗气,但如果有人知道怎么做,我会很感激它以供将来引用。

我也对 google 进行了广泛的搜索,我可能只是使用了错误的搜索词,但我也找不到任何东西。

非常感谢任何帮助。

非常感谢。

Michaeljvdw

- (void)countDownMethod {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:startDay];
[comps setMonth:startMonth];
[comps setYear:startYear];
[comps setHour:startHour];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSLog(@"%@",date);
[gregorian release];
[comps release];
NSTimeInterval diff = [date timeIntervalSinceNow];

int diffInt = diff;

NSString *days = [NSString stringWithFormat:@"%d",diffInt/86400];
day0.text = @"0";
day1.text = @"0";
day2.text = @"0";
NSLog(@"Days Length: %d",days.length);

if(days.length >= 1){
day2.text = [days substringFromIndex:days.length - 1];

if(days.length >= 2){
day1.text = [days substringWithRange:NSMakeRange(days.length - 2, 1)];

if(days.length >= 3){
day0.text = [days substringWithRange:NSMakeRange(days.length - 3, 1)];
}
}
}

NSString *hours = [NSString stringWithFormat:@"%d",(diffInt%86400)/3600];
hour0.text = @"0";
hour1.text = @"0";
NSLog(@"Hours Length: %d",hours.length);

if(hours.length >= 1){
hour1.text = [hours substringFromIndex:hours.length - 1];

if(hours.length >= 2){
hour0.text = [hours substringWithRange:NSMakeRange(hours.length - 2, 1)];
}
}
NSString *minutes = [NSString stringWithFormat:@"%d",((diffInt%86400)%3600)/60];
minute0.text = @"0";
minute1.text = @"0";
NSLog(@"Minutes Length: %d",minutes.length);

if(minutes.length >= 1){
minute1.text = [minutes substringFromIndex:minutes.length - 1];

if(minutes.length >= 2){
minute0.text = [minutes substringWithRange:NSMakeRange(minutes.length - 2, 1)];
}
}

最佳答案

如果您知道 2 个日期之间的时间(以秒为单位)(您的 NSTimeInterval),那么您可以轻松地将其转换为格式为 days:hours:mins:secs 的字符串,如下所示。

- (NSString*)secsToDaysHoursMinutesSecondsString:(NSTimeInterval)theSeconds {
div_t r1 = div(theSeconds, 60*60*24);
NSInteger theDays = r1.quot;
NSInteger secsLeftFromDays = r1.rem;

div_t r2 = div(secsLeftFromDays, 60*60);
NSInteger theHours = r2.quot;
NSInteger secsLeftFromHours = r2.rem;

div_t r3 = div(secsLeftFromHours, 60);
NSInteger theMins = r3.quot;
NSInteger theSecs = r3.rem;

NSString* days;
if (theDays < 10) { // make it 2 digits
days = [NSString stringWithFormat:@"0%i", theDays];
} else {
days = [NSString stringWithFormat:@"%i", theDays];
}

NSString* hours;
if (theHours < 10) { // make it 2 digits
hours = [NSString stringWithFormat:@"0%i", theHours];
} else {
hours = [NSString stringWithFormat:@"%i", theHours];
}

NSString* mins;
if (theMins < 10) { // make it 2 digits
mins = [NSString stringWithFormat:@"0%i", theMins];
} else {
mins = [NSString stringWithFormat:@"%i", theMins];
}

NSString* secs;
if (theSecs < 10) { // make it 2 digits
secs = [NSString stringWithFormat:@"0%i", theSecs];
} else {
secs = [NSString stringWithFormat:@"%i", theSecs];
}

return [NSString stringWithFormat:@"%@:%@:%@:%@", days, hours, mins,secs];
}

关于objective-c - 倒数日子 - iPhone倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3177729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com