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!
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.
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) { } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With