Gwyn Denali
Gwyn Denali

Reputation: 11

Simulatenous independent gestures

I am making a game where player moves with two figures at a time. Each one has its own half of the screen and moves just within in. Unfortunately I found out that when I swipe with both thumbs at a time nothing happens. Not even one of my recognizers are being triggered.

Maybe there is one way. I made another two views on the top of the GameViewController and added separate gestures. But I cant reffer to them in my gamescene.m to create actions.

Is there anyway to recognize swipes declared in GameViewController, in GameScene and add to them any actions?

I've already tried to make my own recognizers according to touch began and ended but when two fingers are being released at a time it got messy and usually forgot to react twice, I mean for each release separately.

-(void)setUpGestActions{
    _swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft:)];
    [self.swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    self.swipeGestureLeft.cancelsTouchesInView = NO;
    self.swipeGestureLeft.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureLeft];

    _swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight:)];
    [self.swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
    self.swipeGestureRight.cancelsTouchesInView = NO;
    self.swipeGestureRight.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureRight];

    _swipeGestureUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp:)];
    [self.swipeGestureUp setDirection:UISwipeGestureRecognizerDirectionUp];
    self.swipeGestureUp.cancelsTouchesInView = NO;
    self.swipeGestureUp.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureUp];

    _swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown:)];
    [self.swipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown];
    self.swipeGestureDown.cancelsTouchesInView = NO;
    self.swipeGestureDown.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureDown];

    _moveLeft = [SKAction moveByX:-self.frame.size.width/6 y:0 duration:self.velocity];
    _moveRight = [SKAction moveByX:self.frame.size.width/6 y:0 duration:self.velocity];
    _moveUp = [SKAction moveByX:0 y:self.frame.size.width/6  duration:self.velocity];
    _moveDown = [SKAction moveByX:0 y:-self.frame.size.width/6 duration:self.velocity];

    _downMovement = [SKAction moveByX:0 y:-1 duration:self.downMovementVelocity];
}

-(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveLeft];
    }
    else{
        [_girl runAction:self.moveLeft];

    }
}

-(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveRight];
    }
    else{
        [_girl runAction:self.moveRight];
    }
}

-(void)swipeUp:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveUp];
    }
    else{
        [_girl runAction:self.moveUp];
    }
}

-(void)swipeDown:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveDown];
    }
    else{
        [_girl runAction:self.moveDown];

    }
}

Upvotes: 1

Views: 61

Answers (3)

Gwyn Denali
Gwyn Denali

Reputation: 11

The simplest solution is to divide screen into two minor views and attach separate gesture recognizers to each one.

Upvotes: 0

Sabby
Sabby

Reputation: 2592

I think you can look at this example. It might help you.

UIScreenEdgePanGestureRecognizer *myScreenEdgePanGestureRecognizer;
...
myScreenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgePan:)];
myScreenEdgePanGestureRecognizer.delegate = self;
// Configure the gesture recognizer and attach it to the view.
...
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    BOOL result = NO;
    if ((gestureRecognizer == myScreenEdgePanGestureRecognizer) && [[otherGestureRecognizer view] isDescendantOfView:[gestureRecognizer view]]) {
        result = YES;
    }
    return result;
 }

Go through this link, you will find more information.

https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html

Upvotes: 0

jrturton
jrturton

Reputation: 119292

To recognise multiple gestures at the same time, set a delegate on each gesture recogniser. The delegate can be the same object for each gesture.

In the delegate, implement this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
     shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

Upvotes: 1

Related Questions