0

I want to make zoom function by PinchGestureRecognizer. I can scale by this code,but imageView is restored whenever I pinch out. I want to make the function that If I scale imageView sometimes,it isn't restored every time. And the code scrollView.minimumZoomScale = 3.0,scrollView.maximumZoomScale = 6.0 don't work.

What should I do?

Here is my code

        scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        scrollView.frame = self.view.bounds;
        scrollView.backgroundColor = [UIColor blueColor];
        scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height);
        scrollView.frame = CGRectMake(0,50,320,500);
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        scrollView.pagingEnabled = YES;
        scrollView.bouncesZoom = YES;
        scrollView.minimumZoomScale = 3.0;
        scrollView.maximumZoomScale = 6.0;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:scrollView];

        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
                                                  initWithTarget:self action:@selector(handlePinchGesture:)];
        [scrollView addGestureRecognizer:pinchGesture];

            img =[UIImage imageNamed:[NSString stringWithFormat:@"a.jpg"]];
            imgView = [[UIImageView alloc]initWithImage:img];
            imgView.frame = CGRectMake(0,0, self.view.frame.size.width, 448);
            imgView.userInteractionEnabled = YES;
            [scrollView imgView];


- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {
    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    imgView.transform = CGAffineTransformMakeScale(factor, factor);
    NSLog(@"factor %f",factor);

}
Shebin Koshy
  • 1,182
  • 8
  • 22
zmbdr
  • 115
  • 1
  • 5
  • possible duplicate of [Max/Min Scale of Pinch Zoom in UIPinchGestureRecognizer - iPhone iOS](http://stackoverflow.com/questions/5150642/max-min-scale-of-pinch-zoom-in-uipinchgesturerecognizer-iphone-ios) – Anbu.Karthik Sep 07 '15 at 06:35
  • I could do it!Thank you very much! – zmbdr Sep 07 '15 at 06:49

3 Answers3

0

try use CGAffineTransformScale instead like:

imgView.transform = CGAffineTransformScale(imgView.transform, factor, factor);
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • minimumZoomScale doesnt have an effect when using your own pinch gesture i think. after doing the transform, make sure the frame of `imgView` is within some defined limits, eg, make sure the width < 600 and height < 400 or whatever, if its bigger, then reduce its size. maybe [something like this](http://stackoverflow.com/a/5154514/1219956) – Fonix Sep 07 '15 at 06:50
0

Try This Code

    UIScrollView   *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,imageViewHeight)];
    scrollview.showsVerticalScrollIndicator=YES;
    scrollview.scrollEnabled=YES;
    scrollview.maximumZoomScale = 3.0;
    scrollview.delegate = self;
    scrollview.userInteractionEnabled=YES;
    [self.view addSubview:scrollview];`
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Jugal K Balara
  • 917
  • 5
  • 15
0

NOTE: this is just the another way to achieve your requirement

one thing if u are using UIScrollView there is no need to add UIPinchGestureRecognizer to scroll view by default it has pinch gesture.

for zooming there is a delegate methods need to override

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

and

- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center

for example

    //initilization
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.frame = self.view.bounds;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height);
    scrollView.frame = CGRectMake(0,50,320,500);
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.pagingEnabled = YES;
    scrollView.bouncesZoom = YES;
    scrollView.minimumZoomScale = 3.0;
    scrollView.maximumZoomScale = 6.0;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;

    scrollView.delegate = self; /* add this line */

    [self.view addSubview:scrollView];


//this view will be scaled accordingly
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imgView; //return the view which is the subview of scroll view need to be zoomed 
}

//it is the rect for the scaling
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center
{
   CGRect zoomRect;

   zoomRect.size.height = scrollView.frame.size.height / scale;
   zoomRect.size.width  = scrollView.frame.size.width  / scale;

   zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
   zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
   return zoomRect;
}
Shankar BS
  • 8,394
  • 6
  • 41
  • 53