27

I have a CCSprite that I want to move around using gestures. Problem is I'm completely new to Cocos2D. I want my sprite to perform one action when the gesture is up, another one when gesture is down, another action when gesture is right and same thing for left. Can someone point me in the right direction?

Thanks!

LearnCocos2D
  • 63,754
  • 20
  • 125
  • 213
Magnus
  • 1,444
  • 5
  • 21
  • 30

7 Answers7

59

Apparently each UISwipeGestureRecognizer can only detect the swipe in the given direction. Even though the direction flags could be OR'ed together the UISwipeGestureRecognizer ignores the additional flags.

The solution is to add one UISwipeGestureRecognizer for each direction you want the swipe gesture to be recognized, and set each recognizer's direction accordingly to either up, down, left and right. If you want to test for a swipe in any direction you'll have to add four UISwipeGestureRecognizers.

It's kind of odd but that's the only way it worked for me.

LearnCocos2D
  • 63,754
  • 20
  • 125
  • 213
  • That does work great if you only need swipes. If you need to recognize swipes and a pan at the same time, see my answer here: http://stackoverflow.com/questions/8181774/how-to-recognize-swipe-in-all-4-directions/20734326#20734326 – Alex the Ukrainian Dec 22 '13 at 21:42
28
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
[self.gestureAreaView addGestureRecognizer:swipeGesture];
[swipeGesture release];

-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender 
{
    //Gesture detect - swipe up/down , can't be recognized direction
}

or

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
[swipeGesture release];

UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeGesture2];
[swipeGesture2 release];

-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender 
{
    //Gesture detect - swipe up/down , can be recognized direction
    if(sender.direction == UISwipeGestureRecognizerDirectionUp)
    {
    }
    else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
    {
    }
}
DevGom
  • 289
  • 3
  • 6
  • Thanks for posting an answer! While a code snippet could answer the question it's still great to add some addition information around, like explain, etc .. – j0k Sep 24 '12 at 19:25
8

Use a UIPanGestureRecogizer and detect the swipe directions you care about. see the UIPanGestureRecognizer documentation for details. -rrh

// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[panRecognizer setDelegate:self];
[self addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on


-(void)panRecognized:(UIPanGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
        // you might want to do something at the start of the pan
    }

    CGPoint distance = [sender translationInView:self]; // get distance of pan/swipe in the view in which the gesture recognizer was added
    CGPoint velocity = [sender velocityInView:self]; // get velocity of pan/swipe in the view in which the gesture recognizer was added
    float usersSwipeSpeed = abs(velocity.x); // use this if you need to move an object at a speed that matches the users swipe speed
    NSLog(@"swipe speed:%f", usersSwipeSpeed);
    if (sender.state == UIGestureRecognizerStateEnded) {
        [sender cancelsTouchesInView]; // you may or may not need this - check documentation if unsure
        if (distance.x > 0) { // right
            NSLog(@"user swiped right");
        } else if (distance.x < 0) { //left
            NSLog(@"user swiped left");
        }
        if (distance.y > 0) { // down
            NSLog(@"user swiped down");
        } else if (distance.y < 0) { //up
            NSLog(@"user swiped up");
        }
        // Note: if you don't want both axis directions to be triggered (i.e. up and right) you can add a tolerence instead of checking the distance against 0 you could check for greater and less than 50 or 100, etc.
    }
}
Richie Hyatt
  • 2,044
  • 2
  • 19
  • 14
  • Interesting solution, but it bears mentioning that a big part of the point of `UIGestureRecognizer` and its subclasses is to standardize the way gestures are implemented... your code includes and suggests using your own logic to discriminate between swipes of different directions, speeds, etc. which means you run the risk of having a gesture implementation that "feels different" from the one people are used to in iOS. That said, this may be a good way for some cases, such as when you want to make the swiped/panned object move while being swiped. – S'pht'Kr Aug 26 '13 at 11:49
7

The defaut direction is UISwipeGestureRecognizerDirectionRight. the multiple directions also can be specified like that :

[swipeGesture setDirection: UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft];

/// But if you want to get every single direction ,like that:

 UISwipeGestureRecognizer *swipeGestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureRight:)];
[swipeGestureR setDirection: UISwipeGestureRecognizerDirectionRight ];
 [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureR];

[swipeGestureR release];

UISwipeGestureRecognizer *swipeGestureL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
[swipeGestureL setDirection: UISwipeGestureRecognizerDirectionLeft];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureL];

[swipeGestureL release];

the function handleSwipeGestureLeft will be called when swipe to left,and handleSwipeGestureRight wil be called when you swipe to right

DD_
  • 6,881
  • 11
  • 36
  • 60
roundsun
  • 71
  • 1
  • 2
1

Add one UISwipeGestureRecognizer for each axe (horizontal and vertical):

UISwipeGestureRecognizer *horizontalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(action)];
[horizontalSwipe setDirection:(UISwipeGestureRecognizerDirectionRight |
                           UISwipeGestureRecognizerDirectionLeft )];

[self.view addGestureRecognizer:horizontalSwipe];

UISwipeGestureRecognizer *verticalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(action)];
[verticalSwipe setDirection:(UISwipeGestureRecognizerDirectionUp |
                     UISwipeGestureRecognizerDirectionDown )];

[self.view addGestureRecognizer:verticalSwipe];
Damien Romito
  • 8,463
  • 8
  • 57
  • 71
0

Even though there's lots of good info here, I couldn't find a quick answer that had it all.

If you want to differentiate whether a swipe is left or right or up or down, you need to create a new UISwipeGestureRecognizer for each direction.

However! This is not so bad, because you can route each of your gesture recognizers to the same selector, that can then use a switch statement as you might expect.

First, add gesture recognizers for each direction and route them to the same selector:

- (void)setupSwipeGestureRecognizers
{
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipeScreen:)];
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipeScreen:)];
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:rightSwipeGestureRecognizer];
    [self.view addGestureRecognizer:leftSwipeGestureRecognizer];
}

Second, differentiate between the directions with a switch statement:

- (void)userDidSwipeScreen:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    switch (swipeGestureRecognizer.direction) {
        case UISwipeGestureRecognizerDirectionLeft: {
            // Handle left
            break;
        }
        case UISwipeGestureRecognizerDirectionRight: {
            // Handle right
            break;
        }
        default: {
            break;
        }
    }
}
kgaidis
  • 10,753
  • 4
  • 58
  • 76
0
-(void)addGesture {
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    [self.view addGestureRecognizer:swipeGesture];
    [swipeGesture release];
}

-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender {

if (sender.direction == UISwipeGestureRecognizerDirectionUp) {
  //do something
 }
else if (sender.direction == UISwipeGestureRecognizerDirectionDown) {
  //do something
 }
else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
  //do something
 }
else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
  //do something
 }


}

Can also use a switch instead of all the if statements

Zigglzworth
  • 6,265
  • 7
  • 59
  • 101
  • I added this, but it only detects when the direction is right. I added the gesture like this: -(void)addGesture { UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGesture]; [swipeGesture release]; } – Magnus Sep 15 '11 at 08:38
  • Put NSLog("Gesture direction: %@", sender.direction); at the start of the -(void)handleSwipeGesture method and see what you get when you swipe in all directions... its strange that it only recognizes right. – Zigglzworth Sep 15 '11 at 12:07
  • If I NSLog it, the output is: 1 which is the value for right. Is there any other way to detect gestures in cocos2d ? – Magnus Sep 15 '11 at 12:38
  • gesture.direction is RIGHT by default, thats why you are getting only RIGHT (1). – Nishant Apr 06 '16 at 18:25