0

当试图获得时间时,我没有得到预期的价值。

当我运行这个时,时间是 19:59。而在 timeDifferenceString 中,Value 为 71976.894206。我认为这是当前的时间。
我计算
71976 / 3600 = 19,99 h
0,99 * 60 = 59,4 m
0,4 * 60 = 24 s

所以我得到了 19:59:24 点的时间。但我想得到第一次和第二次之间的差异,而不是当前时间(以秒为单位)。

我只想获得我(按住)按下按钮
的时间和未按下按钮的时间
(如果我按下按钮则开始第一次,如果按下另一个按钮则停止)。

- (IBAction)pressTheButton:(id)sender {
NSDate * now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss:SSS"];
NSString *currentTime = [formatter stringFromDate:now];    

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components; //= [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:10];

NSDate* firstDate = [NSDate date];
NSString *getTime = [formatter stringFromDate:now];

getTime = [formatter stringFromDate:firstDate];

if (iX==0)
{
    components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
    firstDate = [calendar dateFromComponents:components];
    getTime = [formatter stringFromDate:firstDate];
    //firstDate = [NSDate date];//[dateFormatter dateFromString:@"00:01:00:090"];
}

components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
//NSDate* secondDate = [dateFormatter dateFromString:@"10:01:02:007"];
NSDate * secondDate = [calendar dateFromComponents:components];
getTime = [formatter stringFromDate:firstDate];

NSTimeInterval timeDifference = [firstDate timeIntervalSinceDate:secondDate];
NSString *timeDifferenceString = [NSString stringWithFormat:@"%f", timeDifference];

timeDiff[iX] = [timeDifferenceString intValue];
[timeLabel setText:timeDifferenceString];
iX++;
}

如果您对我的问题有更好的解决方案,请帮助我。

4

1 回答 1

2

在此处查看 UIControlEventTouchDown、UIControlEventTouchUpInside:http: //developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html

然后我想你想存储一个 NSDate,当你按下某种 ivar 或属性(UIControlEventTouchDown)时,当你释放按钮(UIControlEventTouchUpInside 或 UIControlEventTouchUpOutside)然后在那个日期调用 -timeIntervalSinceDate: 时存储另一个。所以它看起来大致是这样的:

- (IBAction)pressedDown:(id)sender {
    self.pressedDownDate = [NSDate date];
}

- (IBAction)touchedUp:(id)sender {
    NSDate *now = [NSDate date];
    NSLog(@"Time passed: %d", [now timeIntervalSinceDate:self.pressedDownDate]);
}
于 2013-07-30T19:48:02.037 回答