【问题标题】:Get difference in hundredth second from NSDate从 NSDate 获得百分之一秒的差异
【发布时间】:2012-03-04 11:35:45
【问题描述】:

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

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. 问题是我发现 endtime 和 starttime 都是一样的,无论我触摸多久,但如果我删除 timeIntervalSince1970 ,它不是

  2. 有什么方法可以得到这种差异

【问题讨论】:

    标签: ios objective-c nsdate


    【解决方案1】:

    我最好用

    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);
    }
    

    【讨论】:

    • @chings228:如果您决定采用这种方式,请不要忘记将 QuartzCore 框架添加到您的项目中
    • 差异的单位是什么 - 是以纳秒还是毫秒或秒为单位 - 当差异只有大约两秒时,我得到一个巨大的负数 Diff - -3345790976.000000
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2012-01-13
    • 1970-01-01
    • 2020-06-13
    • 2012-12-11
    相关资源
    最近更新 更多