【问题标题】:How to add year and month in count down Timer Objective C如何在倒计时计时器目标C中添加年份和月份
【发布时间】:2019-02-12 10:48:31
【问题描述】:

现在我添加了秒和分钟。在我的计时器中。此外,我需要在我的计时器中添加小时月份和年份,如下图所示:

这是我已经实现的代码:

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

它一直工作到分秒。

提前致谢。

【问题讨论】:

标签: ios objective-c nstimer nstimeinterval


【解决方案1】:

添加到 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"]);
}}

【讨论】:

  • 所以输出会和我想要的一样吗?因为我有时间戳
  • 是的,timerFired 计算剩余的日期组件
  • viewDidLoad 中,我们通过将currHour, currMinute, currSeconds 转换为秒然后添加到当前日期来获取计时器完成工作的日期。然后在timerFired 中获取当前日期并计算剩余时间。
  • 我尝试使用 NSString *interval = [[NSUserDefaults standardUserDefaults] stringForKey:@"event_start_date_time_in_ms"]; NSTimeInterval timeInterval = [interval doubleValue]; NSTimeInterval currentDateSeconds = [[NSDate date] timeIntervalSince1970]; NSTimeInterval targetDateSeconds = currentDateSeconds + timeInterval;; targetDate = [NSDate dateWithTimeIntervalSince1970:targetDateSeconds]; 但我在计时器中得到错误的详细信息
  • 是的,因为event_start_date_time_in_ms 每次运行时都有相同的值,所以currentDateSecondstargetDateSeconds 之间的差距始终相同。我可以建议您计算一下并将targetDateSeconds 保存到userdefaults,然后每次运行时从该保存的值创建targetDate
【解决方案2】:

如果您将 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 有很多单位选项,例如NSCalendarUnitYearNSCalendarUnitMonth,因此您可以在需要时将它们添加到列表中。

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2019-09-08
    • 1970-01-01
    相关资源
    最近更新 更多