选择并拖动切片

时间:2015-10-19 01:36:58

标签: ios objective-c

我正在制作一款涉及点击并拖动图块来创建路径的游戏,类似于热门游戏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来持续检查滑动方向的变化? 任何帮助,将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分: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]];

}

希望这有帮助。