【问题标题】:Detecting 2 swipes without interrupting touch在不中断触摸的情况下检测 2 次滑动
【发布时间】:2013-01-18 14:18:35
【问题描述】:

我正在尝试实现将改变单位方向的游戏控制 移动。所以如果我向右滑动它会向右转,如果我向下滑动它会转向向下等等。

这是 cocos2d 游戏,我正在使用 CCNode+SFGestureRecognizersUISwipeGestureRecognizer

现在我有下一个实现

UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
        [self addGestureRecognizer:rightSwipeGestureRecognizer];
        rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
        rightSwipeGestureRecognizer.delegate = self;
        [rightSwipeGestureRecognizer release];
        
        UISwipeGestureRecognizer *upSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleUpSwipe:)];
        [self addGestureRecognizer:upSwipeGestureRecognizer];
        upSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
        upSwipeGestureRecognizer.delegate = self;
        [upSwipeGestureRecognizer release];
        
        UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
        [self addGestureRecognizer:leftSwipeGestureRecognizer];
        leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        leftSwipeGestureRecognizer.delegate = self;
        [leftSwipeGestureRecognizer release];
        
        UISwipeGestureRecognizer *downSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleDownSwipe:)];
        [self addGestureRecognizer:downSwipeGestureRecognizer];
        downSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
        downSwipeGestureRecognizer.delegate = self;
        [downSwipeGestureRecognizer release];

但问题在于,要识别以下手势,您需要将手指从屏幕上抬起。如果您在第一次滑动后没有抬起手指,它只会识别第一次滑动。

现在:

应该如何:

最好的方法是什么?谢谢!

【问题讨论】:

  • 我不熟悉 cocos2d,但听起来你最好自己跟踪触摸并通过跟踪先前的 x,y 和当前的 x,y 来确定方向。滑动手势通常是一个固定的循环手势(你只做一次)并且不会触发连续运动(否则一切都会是滑动)。在标准的 UIKit 中,你有 touchesMoved: 方法,只要触摸在屏幕上移动,就会调用该方法。不确定 cocos2d 等价物是什么,但可能值得研究。

标签: iphone ios cocos2d-iphone uigesturerecognizer swipe


【解决方案1】:

cancelsTouchesInView 应该会有所帮助,它可以防止在手势识别器识别出手势时取消触摸事件。这将允许其他手势识别器继续检查他们的手势。

  rightSwipeGestureRecognizer.cancelsTouchesInView = NO;
  upSwipeGestureRecognizer.cancelsTouchesInView = NO;
  leftSwipeGestureRecognizer.cancelsTouchesInView = NO;
  downSwipeGestureRecognizer.cancelsTouchesInView = NO;

【讨论】:

  • 谢谢,但这个解决方案在我的情况下不起作用。可能由于 CCNode+SFGestureRecognizers.h 的实现而无法正常工作。我会探索这个
【解决方案2】:

好的,这是我的解决方案。我认为这不是一个很好的解决方案,但在我的情况下它有效,因为我需要在每个时间间隔获得方向。

所以,在我的CCLayer 子类中:

#define SWIPE_LENGTH        60
#define SWIPE_TANGENT       2

...

@property(nonatomic, assign) CGPoint latestLocation;

...

-(id) init
{
    if( (self=[super init]) )
        {
            self.isTouchEnabled = YES;
        }
    return self;
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    self.latestLocation = location;
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    CGPoint delataLocation = CGPointMake(location.x - self.latestLocation.x, location.y - self.latestLocation.y);
    if ( sqrt(pow((delataLocation.x), 2) + pow((delataLocation.y), 2) ) > SWIPE_LENGTH ) {
        if (delataLocation.y > 0 && delataLocation.y > SWIPE_TANGENT * ABS(delataLocation.x))
        {
            [self handleSwipeWithDirestion:SDirectionTypeUp];
        } else if (delataLocation.y < 0 && ABS(delataLocation.y) > SWIPE_TANGENT * ABS(delataLocation.x))
        {
            [self handleSwipeWithDirestion:SDirectionTypeDown];
        } else if (delataLocation.x > 0 && delataLocation.x > SWIPE_TANGENT * ABS(delataLocation.y))
        {
            [self handleSwipeWithDirestion:SDirectionTypeRight];
        } else if (delataLocation.x < 0 && ABS(delataLocation.x) > SWIPE_TANGENT * ABS(delataLocation.y))
        {
            [self handleSwipeWithDirestion:SDirectionTypeLeft];
        }
        self.latestLocation = location;
    }
}

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2019-02-17
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多