Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • gesture recognition - Pinch, Pan, Drag and Rotate.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 547
    Comment on it

    The evolution of touch screen devices has been very rapid along the recent years. One of them is the gesture recognition. The gesture recognition technology is able to recognize many type of gestures made by a single finger or even a multi-finger gesture. These gestures can be used to perform a wide variety of functions.The good news is that the same gesture can be used to perform a different function in different applications. For example a pinch gesture can be used in a music player to increase or decrease the volume of the sound track and the same gesture can be used in an image processor to zoom in the image or zoom out of the image.

    A few of the most popular and most widely used features are as follows:

    • Pan gesture (commonly known as the drag gesture).
    • Pinch gesture (commonly used to zoom in or zoom out of the image).
    • Rotation gesture (commonly used in image processors to rotate an image at an angle).
    • Tap gesture (commonly used to select an item and display the available options).

    All of the above mentioned gestures can be implemented and used by adding the respective gestures in the .Xib file.

    Add an -(IBAction) function in the .h file of the ViewController and Connect the functions to the gesture in the .Xib file.

    - (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;
    
    - (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer;
    
    - (IBAction)handleRotate:(UIRotationGestureRecognizer *)recognizer;
    
    - (IBAction)handleTap: (UITapGestureRecognizer *) recognizer;
    

    The code required in .m file to make the gestures work is as follows.

    - (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
    {   
        CGPoint translation = [recognizer translationInView:self.view];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                             recognizer.view.center.y + translation.y);
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    
    }
    
    - (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
    {
        recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
        recognizer.scale = 1;
    }
    
    - (IBAction)handleRotate:(UIRotationGestureRecognizer *)recognizer
    {
        recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
        recognizer.rotation = 0;
    }
    
    - (IBAction)handleTap: (UITapGestureRecognizer *) recognizer;
    {    
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
        singleTap.numberOfTapsRequired = 1;
        [self.view addGestureRecognizer:singleTap];
        NSLog(@"single tap");
        UIAlertView *view1 = [[UIAlertView alloc]initWithTitle:@"single tap" message:@"image tapped 1 time" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
       [view1 show];
    }
    

    PS. Don't forget to include the UIGestureRecognizerDelegate in the .h file of your ViewController.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: