2015-10-19 128 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]]; 

}

希望這有助於。