gpt4 book ai didi

ios - 倒数计时器不会在00:00:00:00:00停止

转载 作者:行者123 更新时间:2023-12-01 16:40:01 24 4
gpt4 key购买 nike

我使用的是Xcode 5.1,并且具有一个倒计时计时器,该计时器不会在00:00:00:00停止计时,并且会一直递减为负数,请您帮助以下代码显示:

请注意,datelabel.text是计时器的显示位置。

-(void)updatelabel{
NSCalendar *calender= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calender components:units fromDate:[NSDate date] toDate:destinationdate options:1];

[datelabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm',[components day], 'd',[components hour], 'h',[components minute], 'm',[components second], 's']];
}


if ([store.offer isEqual:@"No"] || [store.offerexp isEqual:@"3600"] ) {
datelabel.text =@"xx";
} else {
destinationdate = [NSDate dateWithTimeIntervalSince1970:store.offerexp.intValue];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatelabel) userInfo:nil repeats:YES];

NSLog(@"destination%@",destinationdate);
}

NSTimeInterval TimeStamp = [[NSDate date] timeIntervalSince1970];

最佳答案

到达destinationdate后停止计时器。

- (void)updateLabel:(NSTimer *)timer {
NSDate *now = [NSDate date];
if ([now compare:destinationdate] != NSOrderedAscending) {
NSCalendar *calender= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calender components:units fromDate:now toDate:destinationdate options:1];

[datelabel setText:[NSString stringWithFormat:@"%dm %dd %dh %dm %ds", [components month], [components day], [components hour], [components minute], [components second]]];
} else {
[timer invalidate];
dateLabel.text = @"0m 0d 0h 0m 0s";
}
}

并将计时器选择器更新为 @selector(updateLabel:)而不是 @selector(updatelabel)

另请注意,格式字符串可以直接包含时间单位。

注意-我可能会向后检查 if。它可能需要是 NSOrderedDescending

关于ios - 倒数计时器不会在00:00:00:00:00停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220797/

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