Search code examples
iphoneioscocos2d-iphoneuigesturerecognizerswipe

Detecting 2 swipes without interrupting touch


I am trying to implement game control that will be changes direction of unit movement. So if i do right swipe it turns to the right, if i do down swipe it turns to the down direction etc.

It's cocos2d game, and i'am using CCNode+SFGestureRecognizers with UISwipeGestureRecognizer.

At now I have next implementation for it

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

but the problem is that for to recognize the following gesture you need to lift your finger from the screen. if you do not lift a finger after the first swipe, it will recognize only the first swipe.

at now:

enter image description here

How it should be:

enter image description here

What the best way to do that? Thanks!


Solution

  • Ok, there is my solution. I dont think that is really good solution, but in my case it works, because i need getting direction at every time interval.

    So, in my CCLayer subclass:

    #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;
        }
    }