When you press the screen, drag, and then take your finger off the screen in any particular direction, it is called a swipe gesture. To detect such gestures, there are in-built functions in iOS and Cocos2d that can be used.
Before we start, it is better that we create a project and make the usual changes to the project so that it can load the MainScene
class and select images based on prefix names instead of folder names. As we have already done this in the previous chapters, it is redundant to repeat it. If you have problems, you can refer to the previous chapter and learn it well as we will be creating at least one new project for each chapter in the book.
The MainScene.m
file should look similar to the following:
#import "MainScene.h" @implementation MainScene +(CCScene*)scene{ return[[self alloc]init]; } - (void)onEnter{ [superonEnter]; self.userInteractionEnabled = YES; } - (void)onExit{ [superonExit]; self.userInteractionEnabled = NO; } -(id)init{ if(self = [super init]){ CGSizewinSize = [[CCDirectorsharedDirector]viewSize]; CGPoint center = CGPointMake(winSize.width/2, winSize.height/2); self.contentSize = winSize; } return self; } @end
We will add the onEnter
and onExit
functions to it as we will be interacting with this layer. So, it is required to enable user interaction once the class is initialized, and it is better if we remove it once the class is not required anymore.
We can swipe in four directions: up, left, right, and down. The gesture in each direction has a corresponding gesture recognizer, and this has to be created for each gesture direction. Also, to the recognizer, you need to provide a function that will be responsible for taking any action that the program needs to perform once the gesture is recognized.
In case of the gesture, UISwipeGestureRecognizer
is used to recognize the gesture. We will ask the code of the application to check a gesture in a certain direction—up, down, left, or right—and a corresponding function to take action after the gesture.
In the init
function of the class, add the following lines of code:
UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)]; swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp; [[UIApplicationsharedApplication].delegate.windowaddGestureRecognizer:swipeUpGestureRecognizer];
Here, we created a new UISwipeGestureRecognizer
gesture and named it swipeUpGestureRecognizer
. We initialized it and passed it as the function that corresponds to this gesture.
Next, we will set the gesture recognizer direction to UISwipeGestureRecognizerDirectionUp
to look for a swipe in the upward direction.
Then, we will add the swipe gesture recognizer to the current delegate.
As we passed in the name of a function for gesture handing, let's add this to the class, as follows:
- (void)handleSwipeUpFrom:(UIGestureRecognizer*)recognizer { NSLog(@"Swipe Up"); }
This will just log out the text output once the gesture is recognized.
Now, we will run the application. If you run it on a device, touch and drag your finger in the upward direction and remove the finger from the screen. Alternatively, you can do the same on the simulator by left–clicking, making a drag gesture in the upward direction, and releasing the left mouse button to create the same effect.
If the gesture motion is recognized properly, you should see the console output light up with the following logged text:
Similar to what we did for the upward swipe, we can also create gesture recognizers for the other three directions and add the corresponding functions to be performed once the gesture is recognized.
The following code can be used for the other swipe directions:
//swipe down UISwipeGestureRecognizer* swipeDownGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipeDownFrom:)]; swipeDownGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown; [[UIApplicationsharedApplication].delegate.windowaddGestureRecognizer:swipeDownGestureRecognizer]; //swipe left UISwipeGestureRecognizer* swipeLeftGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipeLeftFrom:)]; swipeLeftGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; [[UIApplicationsharedApplication].delegate.windowaddGestureRecognizer:swipeLeftGestureRecognizer]; //swipe Right UISwipeGestureRecognizer* swipeRightGestureRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(handleSwipeRightFrom:)]; swipeRightGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [[UIApplicationsharedApplication].delegate.windowaddGestureRecognizer:swipeRightGestureRecognizer];
Also, here are the corresponding functions for the gestures:
- (void)handleSwipeDownFrom:(UIGestureRecognizer*)recognizer { NSLog(@"Swipe Down"); } - (void)handleSwipeLeftFrom:(UIGestureRecognizer*)recognizer { NSLog(@"Swipe Left"); } - (void)handleSwipeRightFrom:(UIGestureRecognizer*)recognizer { NSLog(@"Swipe Right"); }
18.119.106.135