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];

    }
}
4

2 回答 2

1

要稍后执行一些选择器,您可以使用:

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
于 2012-09-25T11:04:24.127 回答
1

在目标 ci 中使用sleep(6)停止处理,将 sleep(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 秒后触发通知可能对您有帮助

其他

你也使用 NSTimer :-

在 NSTimer 中,您在 if else 条件下检查您的响应数组计数 > 0,并在您的响应数组 > 0 时用 1 秒调用方法,然后调用 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
    }
 }
于 2012-09-25T12:53:13.350 回答