Mahendra
Mahendra

Reputation: 8904

Zoom a specific area of a Scrollview

I'm stuck on a case where I want to zooming a part(rect) of a scrollview programatically not by touch or gestures.

CGRect cropRect = CGRectMake(xPos * [UIScreen mainScreen].nativeScale, yPos * [UIScreen mainScreen].nativeScale, width1 * [UIScreen mainScreen].nativeScale, height1 * [UIScreen mainScreen].nativeScale);
[self.scrlVPhoto zoomToRect:cropRect animated:YES];

I've set delegate and also implemented delegate method.

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)aScrollView {
    return self.containerView;
}

self.containerView is a view that I want to zoom.

I've tried everything and I'm still confused how to get out of this.

Please some one can help me. Any help is appreciated.

Thanks in advance.

Upvotes: 4

Views: 1211

Answers (4)

Mahendra
Mahendra

Reputation: 8904

I am going to answer my own question.

I've searched lots of articles and hours of debugging I've found that when scrollview is zoomed then actually its contentSize is increased its zoomScale remain unchanged.

So I just used transform property of the scrollview's subview (i.e. self.containerView) and set scrollview's contentOffset I got what I was looking for.

self.containerView.transform = CGAffineTransformMakeScale(1.8, 1.8); // containerView is subview of scrollview and 1.8 is zoom scale just for eg.
[self.scrlVPhoto setContentOffset:CGPointMake(self.scrlVPhoto.contentOffset.x, (self.scrlVPhoto.contentOffset.y + cropRect.origin.y)) animated:true];

Upvotes: 1

Vijay Kachhadiya
Vijay Kachhadiya

Reputation: 376

Please check this one. Its may be helpful to you

 -(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{

      //[self.superImage_View setAlpha:0.5];

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

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



         CGFloat currentScale = [[[pinchGestureRecognizer 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-[pinchGestureRecognizer scale]) ; // new scale is in the range (0-1)
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        CGAffineTransform transform = CGAffineTransformScale([[pinchGestureRecognizer view] transform], newScale, newScale);
    [pinchGestureRecognizer view].transform = transform;

    lastScale = [pinchGestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call


}
   if ([pinchGestureRecognizer state] == UIGestureRecognizerStateEnded) {
          [undoList addObject:cropImage];
    }
}

Upvotes: 0

Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

I have Create the UIImageView, set the Zoom option in imageView when double tap or single tap to UIImageView and the code is given below,

viewDidLoad Method:

   UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

    [doubleTap setNumberOfTapsRequired:2];

DoubleTab Method:

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // zoom in
    float newScale = [myscrollview zoomScale] * 2;

    if (newScale > self.myscrollview.maximumZoomScale){
        newScale = self.myscrollview.minimumZoomScale;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

        [myscrollview zoomToRect:zoomRect animated:YES];

    }
    else{

        newScale = self.myscrollview.maximumZoomScale;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

        [myscrollview zoomToRect:zoomRect animated:YES];
    }
}


    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

        CGRect zoomRect;

        // the zoom rect is in the content view's coordinates.
        //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
        //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
        zoomRect.size.height = [myscrollview frame].size.height / scale;
        zoomRect.size.width  = [myscrollview frame].size.width  / scale;

        // choose an origin so as to get the right center.
        zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
        zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

        return zoomRect;
    }

And you need the delegate methods are shown below,

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if (vmov==nil)
    {

    float newwidth;
    float newheight;

    UIImage *image=detaimageview.image;

    if (image.size.height>=image.size.width){
        newheight=detaimageview.frame.size.height;
        newwidth=(image.size.width/image.size.height)*newheight;

        if(newwidth>detaimageview.frame.size.width){
            float diff=detaimageview.frame.size.width-newwidth;
            newheight=newheight+diff/newheight*newheight;
            newwidth=detaimageview.frame.size.width;

        }

    }
    else{
        newwidth=detaimageview.frame.size.width;
        newheight=(image.size.height/image.size.width)*newwidth;

        if(newheight>detaimageview.frame.size.height){
            float diff=detaimageview.frame.size.height-newheight;
            newwidth=newwidth+diff/newwidth*newwidth;
            newheight=detaimageview.frame.size.height;

            myscrollview.clipsToBounds = NO;

        }
    }
    CGRect frame;
    CGFloat screenWidth;
    CGFloat screenHeight;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    screenWidth = screenRect.size.width;
    screenHeight = screenRect.size.height-64;
    frame.size.width = newwidth;
    frame.size.height = newheight;
    self.myscrollview.contentSize = frame.size;
    float x,y;
    x=0;
    y=0;
    if (newheight<screenHeight)
    {
        x =screenHeight/2-newheight/2;
    }
    if (newwidth<screenWidth)
    {
        y =screenWidth/2-newwidth/2;
    }
    self.detaimageview.frame = CGRectMake(y,x,newwidth,newheight);

    return self.detaimageview;
    }

    return nil;
}

this above codes are zoom the imageView and its working for my Projects,

hope its helpful

Upvotes: 0

Apurv
Apurv

Reputation: 17186

You should add the view which you want to zoom inside the scrollview. Inside the delegate method return the view which you want to zoom.

Upvotes: 0

Related Questions