2015-10-19 130 views
0

我试图做一个游戏,涉及点击和拖动瓷砖创建路径,类似于流行的游戏Flow Free.选择和拖动瓷砖

我希望能够选择瓷砖和滑动我的手指都在一个滑动,但我有一些问题。 我一直在使用SwipeGestures尝试,在

// listen for swipes to the left 
UISwipeGestureRecognizer * swipeLeft= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)]; 
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeLeft]; 
// listen for swipes to the right 
UISwipeGestureRecognizer * swipeRight= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)]; 
swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeRight]; 
// listen for swipes up 
UISwipeGestureRecognizer * swipeUp= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp)]; 
swipeUp.direction = UISwipeGestureRecognizerDirectionUp; 
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeUp]; 
// listen for swipes down 
UISwipeGestureRecognizer * swipeDown= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown)]; 
swipeDown.direction = UISwipeGestureRecognizerDirectionDown; 
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeDown]; 

我的问题是SwipeGestures只承认每个屏幕上,按下一个刷卡 - 如果我改变方向,中期刷卡,不登记。

假设我需要使用UIGestureRecognizers,是否可以使用PanGestureRecognizer和SwipeGestureRecognizer来持续检查滑动方向的变化? 任何帮助,将不胜感激。提前致谢!

+0

这是因为您正在为每个滑动方向传递不同的方法,而不是让UIPanGestureRecognizer检测滑动方向。 –

回答

1

你在评估中是正确的:UISwipeGestureRecognizer对此并不是很有用,因为只有在确认一旦滑动完成。

想要的是在滑动发生时跟踪项目,您将使用UIPanGestureRecognizer并跟踪每个移动。

要跟踪哪个方向,你可以做一些与此类似:

- (void)onPan:(UIPanGestureRecognizer *pan) { 
    CGPoint translation = [pan translationInView:[pan view]]; 
    if (translation.x > 0) { 
    // moving right... 
    } 

    // important to "eat" the translation if you've handled the 
    // UI changes, otherwise the translation will keep accumulating 
    // across multiple calls to this method 
    [pan setTranslation:CGPointZero inView:[pan view]]; 

}

希望这有助于。