2011-03-30 37 views
1

當我正在開發一種測量抖動事件的總時間的應用程序所花費的時間。換句話說,定時器在搖動事件開始時啓動,當搖動事件結束時定時器停止。測量震動裝置

所以我的問題是,如果我們認爲在一個手中有兩張iTouches,然後在同一時間都搖,將抖動事件產生兩個iTouches相同的值?

這是我的代碼:

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if (motion == UIEventSubtypeMotionShake) { 
     timer = [NSTimer scheduledTimerWithTimeInterval:0.00025 target:self selector:@selector(showactivity) userInfo:nil repeats:YES]; 
    } 
} 
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if (motion == UIEventSubtypeMotionShake) { 
     [timer invalidate]; 
    } 
} 
+0

爲什麼不呢? – 2011-03-30 02:30:58

+0

我現在正在獲得不同的值。所以想知道這是否總是這種情況。 – user531 2011-03-30 02:33:54

+0

你可以添加一些代碼來衡量你如何測量震動? – 2011-03-30 02:41:40

回答

0

我只是做所謂shakesBegan,或任何NSDate類型的實例變量。然後,只需調用這個在motionBegan

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if (motion == UIEventSubtypeMotionShake) { 
     shakesBegan = [NSDate date]; 
    } 
} 

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if (motion == UIEventSubtypeMotionShake) { 
     NSDate *shakesFinished = [NSDate date]; 
     NSTimeInterval shakeTime = [shakesFinished timeIntervalSinceDate:shakesBegan]; 
     NSLog(@"Shaken for %f seconds",shakeTime); 
    } 
} 

輸出應該這樣說Shaken for 1.233242 seconds。但是,我沒有一個設備來測試,所以你的里程可能會有所不同。


您收到錯誤的原因是因爲您必須將shakesBegan作爲實例變量。像這樣在你的界面:

@interface Something : UIViewController 
{ 
    NSDate *shakesBegan; 
} 
@end 
+0

您在那裏使用的「methodfinish」是什麼? – user531 2011-03-30 03:36:05

+0

糟糕,那應該是'shakesFinished'。 – 2011-03-30 03:43:19

+0

但事情是,它不承認motionEnded中的「shakesBegan」。因爲它已被宣佈開始運作。那就是彈出的錯誤。 – user531 2011-03-30 04:01:17