Showing posts with label UISwipeGesture. Show all posts
Showing posts with label UISwipeGesture. Show all posts

Friday, 29 April 2016

Pan Gesture Recognizer



For basic overview of Gesture Recognizer Click here
We will continue here with Pan Gesture Recognizer. Pan Gesture Recognizer allow to drag view around the screen.In this part, except for implementing the necessary code that will enable our app to support the pan gesture.
The pan gesture is actually a drag gesture. It’s used when it’s needed to drag views from one point to another.

First create a view
UIView *panView =  [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
panView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:panView];

Defining Callback method for Gesture Recognizer

-(void) handlepanGesture:(UIPanGestureRecognizer *)panGesture{
    CGPoint touchPoint = [panGesture locationInView:self.view];
    panView.center = touchPoint;
}


Now run the app and check.
You can drag 'Pan View' around main View.

The approach I presented above is simple for the sake of the tutorial. In a real application, you might want to enrich the movement of the view by adding acceleration or deceleration when you start or stop dragging, or anything else you need. It’s up to you to apply the proper logic in the action method you implement.

You can find points per second the dragged view travels in both the horizontal and vertical axis while dragging by method velocityInView: .
Go to Callback method and add following codes

//find velocity
       CGPoint velocity = [panGesture velocityInView:self.view];
       NSLog(@"Horizontal Velocity: %.2f points/sec",velocity.x);
       NSLog(@"Vertical Velocity: %.2f points/sec",velocity.y);

Now run the app again and drag panView.
you can check horizontal  and vertical velocity on Console Area.

</script>

Friday, 22 April 2016

Swipe Gesture

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.