2012-09-25 38 views
0

我想延遲搖晃5秒鐘,因爲如果用戶持續搖動設備,響應顯示爲空。所以這就是爲什麼我想延遲振動直到n,除非響應還活着。如何延遲搖動通知

這裏是我的代碼是

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { 

    if (motion == UIEventSubtypeMotionShake) { 

     [FlurryAnalytics logEvent:@"User shaked to update"]; 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"CheckWeather" object:nil]; 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"startWeatherNeue" object:nil]; 

     if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) 

      [super motionEnded:motion withEvent:event]; 

    } 
} 

回答

1

在目標詞的使用睡眠(6)爲停止處理, 放睡眠(6 )爲您解僱通知代碼: -

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { 

if (motion == UIEventSubtypeMotionShake) { 

sleep(6); 

[FlurryAnalytics logEvent:@"User shaked to update"]; 

[[NSNotificationCenter defaultCenter] postNotificationName:@"CheckWeather" object:nil]; 

[[NSNotificationCenter defaultCenter] postNotificationName:@"startWeatherNeue" object:nil]; 

if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) 

    [super motionEnded:motion withEvent:event]; 

    } 
} 

,然後處理停止6秒鐘,然後通知起火後6 Seconde系列可能是它有助於您

其他

ü也使用的NSTimer: -

中的NSTimerü檢查您迴應陣列數> 0,如果其他條件和呼叫方法1秒時,你的反應陣列> ​​0,則調用NSNOtification方法

這裏我舉的例子: - 用的NSTimer

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 

    self.TimeOfActiveUser = [NSTimer scheduledTimerWithTimeInterval:01.0 target:self selector:@selector(checkInfoString) userInfo:nil repeats:YES]; 
} 


-(IBAction)checkInfoString 
{ 

    if([responsearray count]>0) 
    { 
    [FlurryAnalytics logEvent:@"User shaked to update"]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"CheckWeather" object:nil]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"startWeatherNeue" object:nil]; 

     if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) 
     { 
      [super motionEnded:motion withEvent:event]; 
     } 

    } 
    else 
    { 

    NSLOG 
    } 
} 
+0

謝謝nitin,我會嘗試在我的要求中檢查這個代碼,看看它是如何工作的! – 2012-09-28 08:41:47

1

要進行一些選擇後,你可以使用:

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay