Detect when UIGestureRecognizer is up, down, left and right Cocos2d

Magnus picture Magnus · Sep 14, 2011 · Viewed 39.1k times · Source

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!

Answer

LearnCocos2D picture LearnCocos2D · Oct 13, 2011

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.