【问题标题】:Selecting and Dragging tiles选择和拖动瓷砖
【发布时间】:2015-10-19 01:36:58
【问题描述】:

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

【问题讨论】:

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

标签: ios objective-c


【解决方案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]];

}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多