GCD Timer在dispatch_suspend上重复触发

时间:2014-12-04 01:36:35

标签: iphone timer cocos2d-iphone grand-central-dispatch

我使用Xcode 5.1和Cocos2d v3.0作为参考。在这个练习应用程序中,我希望用户按住屏幕。如果用户按住一秒钟,程序应该运行" someMethod"。用户每秒按住屏幕,程序应该运行" someMethod"。因此,如果用户在一秒的每个内部按住屏幕总共五秒钟,那么#Method"应该叫。如果用户没有按住屏幕,则时间不应该运行。我想让这个用户按下每个计时器。因此,如果用户按住一秒钟,从屏幕上移开手指,然后再次按住,则计时器应该重置。最后,如果用户按住2.5秒" someMethod"不应该第三次被解雇。

我的问题是,如果我从屏幕上抬起手指,我的重复计时器就不会停止

以下是我输出的示例

2014-12-03 19:47:45.987 Practice App[14739:f03] fire      
2014-12-03 19:47:46.986 Practice App[14739:f03] fire 
//after this point in time I am not holding down on the screen   
2014-12-03 19:47:47.986 Practice App[14739:f03] fire  
2014-12-03 19:47:48.986 Practice App[14739:f03] fire   
2014-12-03 19:47:49.986 Practice App[14739:f03] fire
@implementation GameScene
{
    dispatch_source_t dispatchSource;
}

- (instancetype)init
{

     if (self = [super init]){
    dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
                                                              dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));

    double interval = 1.0;
    dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, 0);
    uint64_t intervalTime = (int64_t)(interval * NSEC_PER_SEC);
    dispatch_source_set_timer(dispatchSource, startTime, intervalTime, 0);

    dispatch_source_set_event_handler(dispatchSource, ^{
        [self someMethod];
    });


  ...
  }
  return self;
}

- (void)fixedUpdate:(CCTime)dt
{
    if(touchState == kTouchDown){//The player is touching the screen
        dispatch_resume(dispatchSource);
    }

    if (touchState == kTouchUp) {//The player isn't touching the screen
        dispatch_suspend(dispatchSource);
    }
}

- (void)someMethod{
    NSLog(@"fire");
}

1 个答案:

答案 0 :(得分:0)

使用5个长按手势识别器,一个将在1秒开火,第二个将在2秒开火等等......这里是第一个的示例,与其他人一起做并调整时间:< / p>

UILongPressGestureRecognizer* recognizerOneSec = 
        [[UILongPressGestureRecognizer alloc] 
                initWithTarget:self
                        action:@selector(handleLongPressFrom:)];
recognizer.minimumPressDuration = 1.0; // seconds
[[[CCDirector sharedDirector] view] 
        addGestureRecognizer:recognizer];

这将在1秒后开火:

-(void)handleLongPressFrom:(UILongPressGestureRecognizer*)recognizer
{
  //Perform here some logic to detect wich gesture will you handle
  //Example: if (recognizer is recognizerOneSec)

    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        CCLOG(@"Long press gesture recognized.");

        // Get the location of the touch in Cocos coordinates.
        CGPoint touchLocation = [recognizer locationInView:recognizer.view];

        //Do something with the touchLocation
    }
}

稍后,请不要忘记这一点,您也可以尝试在每次需要时删除并重新创建手势:

-(void) onExit{
    NSArray *grs = [[[CCDirector sharedDirector] view] gestureRecognizers];

    for (UIGestureRecognizer *gesture in grs){
        if([gesture isKindOfClass:[UILongPressGestureRecognizer class]]){
            [[[CCDirector sharedDirector] view] removeGestureRecognizer:gesture];
        }
    }
}

我想到的另一种方法是创建一个类,使用一个挂起变量并引用该对象,这样你就可以向它发送一个挂起消息,但你必须处理更多的事情,比如可能的内存的问题。