我想存储NSDate中经过的时间量。因此,当一个按钮被触发时,我希望一个计时器开始计数,我猜以秒为单位,然后存储用户在 NSDate 中点击下一个按钮之前经过的时间,以便我可以从另一个 NSDate 中减去此值以获得与 NSTimeinterval 的差异。
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:(slider.value * 60)];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:[date timeIntervalSinceNow] target:self selector:@selector(nextFunction) userInfo:nil repeats:NO];
NSDate
有一个返回NSTimeInterval
的实例方法timeIntervalSinceDate:
。
按下第一个按钮时,可以使用[NSDate date]
获取当前数据并将其存储在名为"previousDate"的属性中,然后在按下第二个按钮时: 您可以再次获取当前数据,并使用以下代码计算传递的时间间隔:
- (void)firstButtonTapped {
[self setPreviousDate:[NSDate date]];
}
- (void)secondButtonTapped {
NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:[self previousDate]];
// timeInterval now contains the amount of time that passed in seconds
}