0

我正在使用 UISwipeGestureRecognizer 在 iPhone 中获取滑动手势。我想在滑动触摸开始和滑动触摸结束时获得 2 个位置点。我已经实现了如下滑动触摸方法

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

    CGPoint touchLocation = [recognizer locationInView:recognizer.view];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];

    CCLOG(@"Hello %@ - %d",NSStringFromCGPoint(touchLocation),recognizer.state);

    if (recognizer.state == UIGestureRecognizerStateBegan) {

        CGPoint touchLocation = [recognizer locationInView:recognizer.view];
        touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
        touchLocation = [self convertToNodeSpace:touchLocation];

        CCLOG(@"UIGestureRecognizerStateBegan %@",NSStringFromCGPoint(touchLocation));

    } else if (recognizer.state == UIGestureRecognizerStateChanged) {    

        CCLOG(@"UIGestureRecognizerStateChanged");

    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint touchLocationEnd = [recognizer locationInView:recognizer.view];
        touchLocationEnd = [[CCDirector sharedDirector] convertToGL:touchLocationEnd];
        touchLocationEnd = [self convertToNodeSpace:touchLocationEnd];
        CCLOG(@"UIGestureRecognizerStateEnded %@",NSStringFromCGPoint(touchLocationEnd));
        }        

    //}
}

我的滑动触摸工作正常。但它只显示 UIGestureRecognizerStateEnded。即使我在屏幕上滑动并且我的触摸还没有结束但 StateEnded 被调用。我如何调用 StateBegin 并获取位置,然后调用 StateEnd。现在只有 StateEnded 正在工作,其他两个 Begin 和 Changing 都不起作用。

4

1 回答 1

1

更新: 我找到原因:

滑动是一个离散的手势,因此每个手势只发送一次相关的动作消息。

而这张图片: 在此处输入图像描述

所以只有三种状态UISwipeGestureRecognizer:可能,识别和结束。但我仍然不知道为什么不调用可能。


我试过你的代码并得到相同的结果。我也不知道为什么会这样。如果你找不到这个问题的解决方案,我建议你使用 for touch 方法自己分析触摸。这肯定可以处理事件,但比使用手势识别器要复杂一些。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
于 2013-02-04T11:21:47.880 回答