倒数日子 - iPhone 倒计时器

发布于 2024-09-07 20:14:36 字数 2351 浏览 4 评论 0原文

我正在尝试制作一个计数器,显示我们出发去欧洲旅行之前的天数。现在只有大约 70 天(截至今天),所以我不相信我应该担心天文数字或任何东西,但我真的很困惑 - 我附上了一些朋友给我的代码,这不也不行。相信我,当我说我已经尝试了我能想到的一切时 - 在有人咬我的头之前(我在这些论坛上看到过这样做),是的,我确实广泛地查看了Apple文档,但我并不是100%确定从哪里开始 - 我尝试过 NSTimer、NSDate 及其所有子类和方法,但没有立即跳出来。

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

我并不是特别想要有一种闪烁的东西,每秒都会更新,直到我们离开 - 就我个人而言,我认为这很俗气,但如果有人知道如何,那么我将不胜感激,以供将来参考。

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

非常感谢任何帮助。

非常感谢。

迈克尔jvdw

- (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)];
    }
}

}

I’m trying to make a counter which shows the number of days until we leave on a trip to Europe. It’s only about 70 days (as of today) so I don’t believe that I should have to worry about astronomically large numbers or anything, but I really am stumped - I’ve attached the code that some friends have given me, which don’t work either. Trust me when I say I’ve tried everything I can think of - and before anyone bites my head off, which I have seen done on these forums, yes I did look very extensively at the Apple Documentation, however I’m not 100% sure where to start - I’ve tried NSTimer, NSDate and all their subclasses and methods, but there’s nothing that jumps out immediately.

In terms of what I think I should actually be doing, I think I need to somehow assign an integer value for the “day” today/ now/ the current day, which will change dynamically using the [NSDate date] and then the same for the date that we leave. The countdown is just updating when the method gets called again (I can do this using NSTimer if need be) and the value that is displayed on the countdown is the differnce between these two values.

I don’t especially want to have a flashing kind of thing that updates every second until we leave - personally I think that’s tacky, but if anyone knows how then I’d appreciate it for future reference.

I’ve also done an extensive search of google, and I may simply be using the wrong search terms, but I can’t find anything there either.

Any help is greatly appreciated.

Thank you very much.

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)];
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-09-14 20:14:39

如果您知道两个日期之间的时间(以秒为单位)(您的 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];
}

If you know the time in seconds between 2 dates (your NSTimeInterval) then you can easily convert that into a string in the format days:hours:mins:secs as follows.

- (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];
}
怕倦 2024-09-14 20:14:39
    //Another simple way  to get the numbers of days difference to a future day from today.

NSTimeInterval todaysDiff = [todayDate timeIntervalSinceNow];
NSTimeInterval futureDiff = [futureDate timeIntervalSinceNow];
NSTimeInterval dateDiff = futureDiff - todaysDiff;

div_t r1 = div(dateDiff, 60*60*24);
NSInteger theDays = r1.quot;

label.text = [NSString stringWithFormat:@"%i", theDays];
    //Another simple way  to get the numbers of days difference to a future day from today.

NSTimeInterval todaysDiff = [todayDate timeIntervalSinceNow];
NSTimeInterval futureDiff = [futureDate timeIntervalSinceNow];
NSTimeInterval dateDiff = futureDiff - todaysDiff;

div_t r1 = div(dateDiff, 60*60*24);
NSInteger theDays = r1.quot;

label.text = [NSString stringWithFormat:@"%i", theDays];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文