我有一个简单的秒表,可以在几秒钟内按下开始按钮后的经过时间。我需要的是从日期选择器设置开始时间。
如何获得“NSTimeInterval时间”;等于我的datepicker?或者我是否需要做其他事情来设置用户的时间?
@implementation ViewController {
bool start;
// Gets time when the button is pressed.
NSTimeInterval time;
}
(无效)viewDidLoad中 { [super viewDidLoad];
self.display.text = @“0:00”;
start = false;
}
- (void)更新{
if (start == false) {
return;
}
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsedTime = currentTime - time;
// minutes.
int minutes = (int)(elapsedTime / 60.0);
// seconds.
int seconds = (int)(elapsedTime = elapsedTime - (minutes * 60));
self.display.text = [NSString stringWithFormat:@"%u:%02u", minutes, seconds];
[self performSelector:@selector(update) withObject:self afterDelay:0.1];
}
答案 0 :(得分:1)
您可以使用:
NSTimeInterval timePicker = [[self.datePicker date] timeIntervalSinceReferenceDate];
NSTimeInterval currentTime = [[NSDate date] timeIntervalSinceReferenceDate];
NSTimeInterval elapsedTime = currentTime - timePicker;
// minutes.
int minutes = (int)(elapsedTime / 60.0);
// seconds.
int seconds = (int)(elapsedTime = elapsedTime - (minutes * 60));
答案 1 :(得分:0)
只需保存开始NSDate. Later get the current
NSDate and use
timeIntervalSinceDate:or
timeIntervalSinceNow`即可获取已用时间。
保存开始时间:
NSTimeInterval startTime = [NSDate date];
随后:
NSTimeInterval elapsedTime = -[[NSDate date] timeIntervalSinceNow];