In this tutorial i am going to show how to use UISwipeGestureRecognizer in the App.
You can find basic overview of Gesture Recognizer in the Previous tutorials.
Click here
By using UISwipeGestureRecognizer we can swipe a view in any of the following directions:
1) Left
2) Right
3) Top
4) Bottom
UISwipeGestureRecognizer class provide a method that allow to specify direction for swipe, if none is set, than right direction used by default.
A Swipe Gesture Recognizer object can monitor and trigger action for specified direction only.
Means, if we want a view in application that support swiping in more than one direction than need to add more than one Swipe Gesture Recognizer to view or you can change direction of Swipe Gesture Recognizer to swipe a view in different direction with one Swipe Gesture.
In this example i will use one Swipe Gesture for swiping a view in left and right both direction.
I assume you know how to create New project, i am not going to show here, mainly i will focus on how to how to use Swipe Gesture in a application.
Firstly i am creating a view.
UIView *swipeView = [[UIView alloc] initWithFrame:self.view.bounds];
swipeView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:swipeView];
Now i am going to define Callback method for Gesture Recognizer
-(void) handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture{
NSLog(@"Swipe Gesture Detected");
}
Adding Gesture to view
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecognizer:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[swipeView addGestureRecognizer:swipeGesture];
Its enough, if you run the app, can see a orange view above the main view. if you swipe orange view to right, Gesture Recognizer triggered and send a message to Callback method.
for confirmation you can check and console, you can see "Swipe Gesture Detected" printed on console.
Now i am going to swipe orange view from left to right (if orange view origin.x is 0) and from right to left(if orange view origin.x is greater than 0).
Go to 'handleSwipeGesture:' and add following code
-(void) handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture{
UISwipeGestureRecognizerDirection direction = swipeGesture.direction;//give specified Direction
UIView *swipeView = swipeGesture.view; //return instance of specified view
//Check Specified Direction
if (direction==UISwipeGestureRecognizerDirectionRight) {
[UIView animateWithDuration:.6 animations:^{
swipeView.frame = CGRectMake(300, swipeView.frame.origin.y, swipeView.frame.size.width, swipeView.frame.size.height);
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; //Change direction
}];
}
else if (direction==UISwipeGestureRecognizerDirectionLeft) {
[UIView animateWithDuration:.6 animations:^{
swipeView.frame = CGRectMake(0, swipeView.frame.origin.y, swipeView.frame.size.width, swipeView.frame.size.height);
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //Change direction
}];
}
else{
NSLog(@"Unknown Direction");
}
}
Now run app again and check.
After swipe toward Right, Orange view slide to right, and again swipe toward Left Orange View slide left.
Note: If you swipe toward Right Orange view slide to right and again you swipe toward right its won't work, because we changed direction from Right to Left.