2012-03-04 59 views
2

獲取百分之一秒的差異我想兩個觸摸從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. 沒有任何方法來獲取差異

回答

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

@ chings228:不要忘記QuartzCore框架添加到您的項目,如果你決定走這條路 – 2012-03-04 13:55:44

+0

添加,thx for ur reminder – chings228 2012-03-05 01:23:27

+0

差異的單位是什麼 - 它是以納秒或毫秒還是秒 - 當差異僅爲約兩秒時,我得到一個巨大的負數 差異 - -3345790976.000000 – OneGuyInDc 2012-11-05 23:58:24