Objective c NSTimer跳跃2秒
请帮忙 我有一个NSTimeInterval,我将其转换为NSDateComponent,然后将每个组件(年、月、日等)指定给一个适当的标签。完成转换后,我启动NSTimer开始倒计时。以下是我使用的代码:Objective c NSTimer跳跃2秒,objective-c,ios7,nstimer,nstimeinterval,Objective C,Ios7,Nstimer,Nstimeinterval,请帮忙 我有一个NSTimeInterval,我将其转换为NSDateComponent,然后将每个组件(年、月、日等)指定给一个适当的标签。完成转换后,我启动NSTimer开始倒计时。以下是我使用的代码: - (void)configureView { if (self.detailItem) { self.progressView.progress = 0.0; self.initialInterval = [[self.detailItem valu
- (void)configureView
{
if (self.detailItem) {
self.progressView.progress = 0.0;
self.initialInterval = [[self.detailItem valueForKey:@"date"] timeIntervalSinceNow];
self.timeElapsed = self.initialInterval;
self.secondsLeft = [self resetLabel];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:YES];
}
}
- (void)timerFireMethod:(NSTimer *)timer
{
if (self.secondsLeft > 0) {
self.secondsLeft--;
self.timeElapsed--;
[self updateSecondsWithNumber:self.secondsLeft];
self.progressView.progress = 1 - (self.timeElapsed / self.initialInterval);
} else {
if (self.timeElapsed <= 0) { [timer invalidate]; return; }
[self resetLabel];
self.secondsLeft = 59;
}
}
- (void)updateSecondsWithNumber:(int)number
{
self.secondLabel.text = [NSString stringWithFormat:@"%ds", number];
}
- (NSTimeInterval)resetLabel
{
NSDateComponents *convertedInfo = [ChronoModel dateComponentFromTimeInterval:self.timeElapsed];
self.yearLabel.text = [NSString stringWithFormat:@"%ldy", (long)convertedInfo.year];
self.monthLabel.text = [NSString stringWithFormat:@"%ldm", (long)convertedInfo.month];
self.dayLabel.text = [NSString stringWithFormat:@"%ldd", (long)convertedInfo.day];
self.hourLabel.text = [NSString stringWithFormat:@"%ldh", (long)convertedInfo.hour];
self.minuteLabel.text = [NSString stringWithFormat:@"%ldm", (long)convertedInfo.minute];
self.secondLabel.text = [NSString stringWithFormat:@"%lds", (long)convertedInfo.second];
return convertedInfo.second;
}
-(无效)配置视图
{
if(self.detailItem){
self.progressView.progress=0.0;
self.initialInterval=[[self.detailItem valueForKey:@“date”]timeIntervalSinceNow];
self.timeappeased=self.initialInterval;
self.secondsLeft=[self resetLabel];
NSTimer*定时器=[NSTimer scheduledTimerWithTimeInterval:1
目标:自我
选择器:@selector(timerFireMethod:)
用户信息:无
重复:是];
}
}
-(void)timerFireMethod:(NSTimer*)计时器
{
如果(self.secondsLeft>0){
赛尔夫特;
self.timepassed--;
[self updateSecondsWithNumber:self.secondsLeft];
self.progressView.progress=1-(self.timeappeased/self.initialInterval);
}否则{
如果(self.timeappeased我的最佳猜测是您有两个计时器实例正在运行。请尝试在[NSTimer scheduledTimerWithTimeInterval
行前面放置一个NSLog。添加一个NSLog(@“hello”);
作为timerFireMethod
的第一行,让我们看看你得到了什么。我添加了一个NSLog来查看secondsLeft和timeExpressed的值,它们似乎衰减得很好。但是NSLog的每一行每秒出现两行。我希望我知道原因:DMy最好的猜测是你有两个计时器实例在运行。尝试在[NSTimer scheduledTimerWithTimeInterval
行之前放置一个NSLog。您的最佳猜测是正确的:)谢谢您,先生!Apple提供的包含configureView的代码调用了两次,因此它不是放置计时器的最佳位置。如果您回答我的问题,我会将其标记为正确的。