Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pinch zooming in image viewer

I have an UIImageView. I need pinch zoom.

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 90, 320, 38)];
[imageView setImage:[UIImage imageNamed:@"accesspanel.png"]];
[self.view addSubview: imageView];
like image 748
janagowtham Avatar asked Mar 13 '14 10:03

janagowtham


2 Answers

You can add the imageview inside a scrollview and can use Scrollview delegate for the this purpose

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];

    self.scrollView.minimumZoomScale=0.5;

    self.scrollView.maximumZoomScale=6.0;

    self.scrollView.contentSize=CGSizeMake(1280, 960);

    self.scrollView.delegate=self;
}
like image 94
Vibhu Srivastava Avatar answered Nov 20 '22 00:11

Vibhu Srivastava


First ---> ADD Pinch Gesture to your Image View -:

UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handlePinchGesture:)];
pgr.delegate = self;
[imageView addGestureRecognizer:pgr];

Second ---> Implement Pinch Gesture -:

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

     if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
     // Reset the last scale, necessary if there are multiple objects with different scales.
     lastScale = [gestureRecognizer scale];
     }

     if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
     [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

      CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

     // Constants to adjust the max/min values of zoom.
     const CGFloat kMaxScale = 2.0;
     const CGFloat kMinScale = 1.0;

      CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]); // new scale is in the range (0-1)
      newScale = MIN(newScale, kMaxScale / currentScale);
      newScale = MAX(newScale, kMinScale / currentScale);
      CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
      [gestureRecognizer view].transform = transform;

      lastScale = [gestureRecognizer scale];  // Store the previous. scale factor for the next pinch gesture call
      }
}
like image 22
Vizllx Avatar answered Nov 20 '22 01:11

Vizllx