2013-04-22 80 views
1

有什麼方法可以限制用戶手勢的持續時間嗎?例如,用戶可以拖動一個精靈,但從cctouch開始,它只能持續3秒。持續時間過後,應用程序將自動觸發cctouch結束方法。如何限制觸摸持續時間?

回答

2

我會建議使用塊調度計時器。避免在Cocos2D中使用NSTimer,因爲它不允許使用內置的暫停/恢復功能。

時間表如下:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    [[self scheduler] scheduleBlockForKey:@"menu" target:self interval:3.0f repeat:0 delay:0 paused:NO block:^(ccTime dt) 
      { 
       // perform end of touch actions here 
      }]; 
} 

還可以肯定,如果用戶做了你想要的任何計時器被調用之前取消預定的塊(ccTouchEnded/ccTouchCancelled很可能):

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    [[self scheduler] unscheduleBlockForKey:@"menu" target:self]; 
} 
+0

尼斯回答,我是新來的科科斯,我不知道有關NSTimers。謝謝 – 2013-04-22 18:21:04

2

是的,這是一個簡單的策略來實現這一目標。您可以在用戶開始實現手勢時啓動計時器,並在計時器命中時停止計時。

-(void) timerDidTick:(NSTimer *)theTimer{ 
    cpMouseRelease(mouse); 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    NSTimer *aTimer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(timerDidTick:) userInfo:nil repeats:NO] ; 
    [[NSRunLoop mainRunLoop] addTimer:aTimer forMode:NSRunLoopCommonModes]; 
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 
    cpMouseGrab(mouse, touchLocation, false); 
... 
}