从NSDate获得百分之一秒的差异

时间:2012-03-04 11:35:45

标签: ios objective-c nsdate

我想在两次触摸之间获得百分之一秒的时差

float starttime;
float endtime;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    starttime = [[NSDate date] timeIntervalSince1970];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

      endtime = [[NSDate date] timeIntervalSince1970];

      NSLog(@"starttime %f endtime %f",starttime,endtime);

      float diff = endtime - starttime;


}
  1. 问题是我发现无论我触摸多长时间,结束时间和开始时间都相同,但如果我删除timeIntervalSince1970,则不是

  2. 是否有任何方法可以实现这种差异

1 个答案:

答案 0 :(得分:2)

我最好用

double startTime = CACurrentMediaTime();

这是推荐的相对时间方式

double starttime;
double endtime;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    startTime = CACurrentMediaTime();
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    endtime = CACurrentMediaTime();
    NSLog(@"starttime %f endtime %f",starttime,endtime);
    double diff = endtime - starttime;
    NSLog(@"Diff - %f", diff);
}