Showing posts with label Touch. Show all posts
Showing posts with label Touch. 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, 8 April 2016

Gesture Recognition


Overview
In iOS 3.0, Apple introduce new UIGestureRecognizer Class! These provide a default implementation of detecting common gestures such as taps, pinches, rotations, swipes, pans, and long presses.
Gesture recognizers convert low-level event handling code into higher-level actions. They are objects that  attach to a view, which allows the view to respond to actions the way a control does. If they recognize their assigned gesture, they send an action message to a target object. The target object is typically the view’s view controller, which responds to the gesture.

A view can have with multiple Gesture Recognitions but a  Gesture Recognizer can't associate with multiple views.

Gesture Recognizer classes of the UIKit framework
Tap (any number of taps) = UITapGestureRecognizer
Pinch  (for zooming)        = UIPinchGestureRecognizer
Pan or Drag                      = UIPanGestureRecognizer
Swipe (any direction)      =  UISwipeGestureRecognizer
Rotating                           =  UIrotationGestureRecognizer
Long Press                       =  UILongPressGestureRecognizer
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

UITapGestureRecognizer:

UITapGestureRecognizer is sub class of UIGestureRecognizer that look for single and multiple taps on associated view.
Adding UITapGestureRecognizer to a view.
1) Creating a Gesture Recognition: You can create a Gesture Recognition by specifying a callback method  so the Gesture Recognition send you update when gesture starts, changes, or ends.
2)Add Gesture Recognition to View : Each gesture is associated with one view.When a touch occurs within the bounds of that view, the gesture recognizer see if it matches the type of touch it’s looking for, and if a match is found it will notify the callback method.

Example:
Adding a Tap Gesture Recognizer to a View:

CallBack method for Gesture Recognizer:
-(void) handleTapGestureRecognizer:(id) sender{
       NSLog("Tap Gesture Occurred");
}

Adding Gesture to view
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecognizer:)];
tapGesture.numberOfTapsRequired = 2;    //Default is 1.
[view addGestureRecognizer:tapGesture];

Note: userInteractionEnabled is always "YES" for specified view.

In the same way you can use rest of Gesture Recognizers.