Iphone Cocos2d中的SwipeGesture

时间:2013-02-04 10:01:55

标签: iphone ios objective-c xcode uiswipegesturerecognizer

我正在使用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正在工作,其他两个开始和改变都不起作用。

1 个答案:

答案 0 :(得分:1)

<强>更新 我找到了原因:

  

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

这张图片: enter image description here

因此UISwipeGestureRecognizer只有三种状态:可能,已识别并已结束。但我仍然不知道为什么可能不被召唤。


我尝试了您的代码并获得了相同的结果。我也不知道为什么会这样。如果您找不到此问题的解决方案,我建议您使用触摸方法自行分析触摸。这肯定可以处理事件,但比使用手势识别器有点复杂。

- (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;