Home > Objective-C > Using Gesture

Using Gesture

 Today I tried to play with Gesture. There are 6 gestures that we can use in IPhone. They are Tap, Pinch, Rotate, Pan, Swipe, and Press. First thing todo is creating IOS Application with XCode (I use XCode 4.4.1), choose Single View Apps. Add 3 image files (IMG_0011.JPG, IMG_0082.JPG, IMG_0093.JPG) in the project.
As usual you have to write code in the ViewController.h and implementation file ViewController.m
Here is ViewController.h. You Need to add UIImageView with interface builder, then link it to imageView variable.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UIImageView *imageView;
}

@property (nonatomic,retain) UIImageView *imageView;

-(IBAction)handleTapGesture:(UIGestureRecognizer *)sender;
-(IBAction)handlePinchGesture:(UIGestureRecognizer *)sender;
-(IBAction)handleRotateGesture:(UIGestureRecognizer *)sender;
-(IBAction)handlePanGesture:(UIGestureRecognizer *)sender;
-(IBAction)handleSwipeGesture:(UIGestureRecognizer *)sender;
-(IBAction)handleLongPress:(UIGestureRecognizer *)sender;

@end

And then implement methods to handle the gestures:
1. Tap Gesture

-(IBAction)handleTapGesture:(UIGestureRecognizer *)sender
{
    if(sender.view.contentMode == UIViewContentModeScaleAspectFit)
    {
        sender.view.contentMode = UIViewContentModeCenter;
    }
    else
    {
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
    }
}

2. Pinch Gesture

-(IBAction)handlePinchGesture:(UIGestureRecognizer *)sender
{
    CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
    if(factor >1)
    {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1), lastScaleFactor + (factor-1));
    }
    else
    {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor);
    }

    if(sender.state == UIGestureRecognizerStateEnded)
    {
        if(factor > 1)
            lastScaleFactor += (factor-1);
        else
            lastScaleFactor *= factor;
    }
}

3. Rotate Gesture

-(IBAction)handleRotateGesture:(UIGestureRecognizer *)sender
{
    CGFloat rotation = [(UIRotationGestureRecognizer *) sender rotation];
    sender.view.transform = CGAffineTransformMakeRotation(rotation + netRotation);
    if(sender.state == UIGestureRecognizerStateEnded)
    {
        netRotation += rotation;
    }
}

4. Pan/Drag Gesture

-(IBAction)handlePanGesture:(UIGestureRecognizer *)sender
{
    CGPoint translation = [(UIPanGestureRecognizer *) sender translationInView:imageView];
    sender.view.transform = CGAffineTransformMakeTranslation(netTranslation.x + translation.x, netTranslation.y + translation.y);

    if(sender.state == UIGestureRecognizerStateEnded)
    {
        netTranslation.x += translation.x;
        netTranslation.y += translation.y;
    }
}

5. Swipe Gesture

-(IBAction)handleSwipeGesture:(UIGestureRecognizer *)sender
{
    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
    switch (direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"UP");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"DOWN");
        case UISwipeGestureRecognizerDirectionRight:
            imageIndex++;
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            imageIndex--;
            break;
        default:
            break;
    }

    imageIndex = (imageIndex < 0) ? ([images count] - 1): imageIndex%[images count];
    imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}

6. Press Gesture

-(IBAction)handleLongPress:(UIGestureRecognizer *)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Image Options"
                                                             delegate:self
                                                    cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save Image", @"Copy Image", nil];
    [actionSheet showInView:self.view];
}

Then Test it, by adding somecode in the viewDidLoad method

- (void)viewDidLoad
{

    // Tap Gesture
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                 action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];

    // Pinch Gesture
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handlePinchGesture:)];
    [imageView addGestureRecognizer:pinchGesture];

    //Rotate Gesture
    UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self
                                                                                              action:@selector(handleRotateGesture:)];
    [imageView addGestureRecognizer:rotateGesture];

    // Panning Gesture
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [imageView addGestureRecognizer:panGesture];

    // Swipping
    images = [[NSArray alloc] initWithObjects:@"IMG_0011.JPG",@"IMG_0082.JPG",@"IMG_0093.JPG", nil];
    UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                     action:@selector(handleSwipeGesture:)];
    [imageView addGestureRecognizer:rightSwipeGesture];

    UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleSwipeGesture:)];
    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:leftSwipeGesture];

    // LongPress Gesture
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longPressGesture.minimumPressDuration = 1;
    longPressGesture.numberOfTouchesRequired = 1;
    longPressGesture.allowableMovement = 15;
    [imageView addGestureRecognizer:longPressGesture];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
Advertisements
Categories: Objective-C
  1. August 9, 2013 at 9:15 am

    I’d prefer to buy the product. Please, I want to know very well what brand and how I can purchase it from your site following your research.Thanks,

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: