Search This Blog

Jul 28, 2011

How to use Gesture Recognizer in iPhone SDK ?

This code is show how to transform image in iPhone SDK using gestures recognizer 


You can download source code from here Download.

1. Code for .h file.

#import


@interface ScaleImageViewController : UIViewController {

    IBOutlet UIImageView *imageView;
    CGFloat lastScale;
}

@end

2. Code for .m file.

#import "ScaleImageViewController.h"


@implementation ScaleImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationController.navigationBar.hidden = TRUE;
   
    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
    [self.view addGestureRecognizer:pinchRecognizer];
}

#pragma mark TapDetectingImageViewDelegate methods

- (void)scale:(id)sender {
   
   
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
    {       
        lastScale = 1.0;
        return;
    }
   
    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
   
    CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
   
    [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
   
    lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

No comments:

Post a Comment