শুক্রবার, ২০ এপ্রিল, ২০১২

iOS Gesture Recognizer: Tap, Pinch Zoom, Rotate, Swipe, Pan, Long Press

The UIGestureRecognizer help us detecting and responding to the various UI gestures of iOS devices. UIGestureRecognizer is an abstract class, which provides the following subclasses:
  1. UITapGestureRecognizer
  2. UIPinchGestureRecognizer
  3. UIRotationGestureRecognizer
  4. UISwipeGestureRecognizer
  5. UIPanGestureRecognizer
  6. UILongPressGestureRecognizer

Gesture Recognizer Examples:

We have defined a recognizer for managing taps. We have specified that there are two taps required, with just one touch (finger). The @selector specifies the method that will be called when the gesture is recognized.

// -----------------------------
// One finger, two taps
// -----------------------------
// Create gesture recognizer
UITapGestureRecognizer *oneFingerTwoTaps = 
  [[[UITapGestureRecognizer alloc] initWithTarget:self action: 
@selector(oneFingerTwoTaps)] autorelease];
 
// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
 
// Add the gesture to the view
[[self view] addGestureRecognizer:oneFingerTwoTaps];
Below is a recognizer for two fingers, two taps:
 
// -----------------------------
// Two fingers, two taps
// -----------------------------
UITapGestureRecognizer *twoFingersTwoTaps = 
  [[[UITapGestureRecognizer alloc] initWithTarget:self action: 
@selector(twoFingersTwoTaps)] autorelease];
[twoFingersTwoTaps setNumberOfTapsRequired:2];
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:twoFingersTwoTaps];
The following gestures recognize swipes up and down:
 
// -----------------------------
// One finger, swipe up
// -----------------------------
UISwipeGestureRecognizer *oneFingerSwipeUp = 
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:
 @selector(oneFingerSwipeUp:)] autorelease];
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[[self view] addGestureRecognizer:oneFingerSwipeUp];
 
// -----------------------------
// One finger, swipe down
// -----------------------------
UISwipeGestureRecognizer *oneFingerSwipeDown = 
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action: 
@selector(oneFingerSwipeDown:)] autorelease];
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[[self view] addGestureRecognizer:oneFingerSwipeDown];
Two recognize two fingers rotating on a view:
 
// -----------------------------
// Two finger rotate  
// -----------------------------
UIRotationGestureRecognizer *twoFingersRotate = 
  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action: 
@selector(twoFingersRotate:)] autorelease];
[[self view] addGestureRecognizer:twoFingersRotate];
And finally, two finger pinch:
 
// -----------------------------
// Two finger pinch
// -----------------------------
UIPinchGestureRecognizer *twoFingerPinch = 
  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:
 @selector(twoFingerPinch:)] autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];

// -----------------------------
// Two finger pan
// ----------------------------- 
panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action: 
@selector(panGestureMoveAround:)] autorelease];
    [panGesture setMaximumNumberOfTouches:2];
    [panGesture setDelegate:self];
 
// -----------------------------
// Long Press Gesture
// ----------------------------- 
UILongPressGestureRecognizer *longPressRecognizer = 
         [[UILongPressGestureRecognizer alloc]
         initWithTarget:self 
         action:@selector(longPressDetected:)];
    longPressRecognizer.minimumPressDuration = 3;
    longPressRecognizer.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:longPressRecognizer];
    [longPressRecognizer release];
 

Gesture Recognizer Action Methods:

For each of the above definitions, below are the associated action methods.
 
/*--------------------------------------------------------------
* One finger, two taps 
*-------------------------------------------------------------*/
- (void)oneFingerTwoTaps
{
  NSLog(@"Action: One finger, two taps");
}
 
/*--------------------------------------------------------------
* Two fingers, two taps
*-------------------------------------------------------------*/
- (void)twoFingersTwoTaps {
  NSLog(@"Action: Two fingers, two taps");
} 
 
/*--------------------------------------------------------------
* One finger, swipe up
*-------------------------------------------------------------*/
- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer 
{ 
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
}
 
/*--------------------------------------------------------------
* One finger, swipe down
*-------------------------------------------------------------*/
- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer 
{ 
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
}
 
/*--------------------------------------------------------------
* Two finger rotate   
*-------------------------------------------------------------*/
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 
{
  // Convert the radian value to show the degree of rotation
  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] *  
(180 / M_PI));
}
 
/*--------------------------------------------------------------
* Two finger pinch
*-------------------------------------------------------------*/
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{
  NSLog(@"Pinch scale: %f", recognizer.scale);
}
 
/*--------------------------------------------------------------
* Two finger pan
*-------------------------------------------------------------*/ 
-(void)panGestureMoveAround:(UIPanGestureRecognizer *)gesture;
{
    UIView *piece = [gesture view];
 
//We pass in the gesture to a method that will help us align our touches
 so that the pan and pinch will seems to originate between the fingers 
instead of other points or center point of the UIView
    [self adjustAnchorPointForGestureRecognizer:gesture];
 
    if ([gesture state] == UIGestureRecognizerStateBegan ||
 [gesture state] == UIGestureRecognizerStateChanged) {
 
        CGPoint translation = [gesture translationInView:[piece superview]];
        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y+ 
translation.y*0.1)];
        [gesture setTranslation:CGPointZero inView:[piece superview]];
    }else if([gestureRecognizer state] == UIGestureRecognizerStateEnded)
    {
       //Put the code that you may want to execute when the UIView became 
       larger than certain value or just to reset them back to their original
       transform scale
    }
} 

/*--------------------------------------------------------------
* Long press
*-------------------------------------------------------------*/ 
- (IBAction)longPressDetected:(UIGestureRecognizer *)sender 
{
    statusLabel.text = @"Long Press";
}