【问题标题】:How to delay the shake notification如何延迟震动通知
【发布时间】:2012-09-25 10:59:05
【问题描述】:

我想将摇动延迟 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];

    }
}

【问题讨论】:

    标签: iphone objective-c xcode xcode4


    【解决方案1】:

    在目标 c 中,我使用 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
        }
     }
    

    【讨论】:

    • 谢谢 nitin,我会尝试在我的需求中检查此代码,看看它是如何工作的!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 2017-06-12
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2021-06-13
      相关资源
      最近更新 更多