Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center an UIImageView on the screen when zoom out

I have an UIImageView inside an UIScrollView. I want that the user can zoom and navigate the image.

This is my work code:

//img is the UIImageView
//scroller is the UIScrollView

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return img; 
}
- (void)viewDidLoad {
   [super viewDidLoad];
   UIImage *image = [UIImage imageNamed:@"map_screen.png"];
   img = [[UIImageView alloc] initWithImage:image];
   scroller.delegate = self;
   scroller.autoresizesSubviews = YES;
   scroller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
   scroller.contentSize = img.frame.size;
   scroller.scrollEnabled = YES;
   scroller.directionalLockEnabled = NO;
   scroller.userInteractionEnabled = YES;

   CGSize ivsize = img.frame.size;
   CGSize ssize = scroller.frame.size;

   float scalex = ssize.width / ivsize.width;
   float scaley = ssize.height / ivsize.height;
   scroller.minimumZoomScale = fmin(1.0, fmax(scaley, scalex));
   scroller.zoomScale = fmin(1.0, fmax(scalex, scaley));
   [scroller addSubview:img];
   img.userInteractionEnabled = YES;
}

all works, but this happened: the minimum zoom is the height of the screen. My image has the width bigger than the height, so i want that the minumum zoom is the width.

If i write

scroller.minimumZoomScale = fmin(1.0, scalex);

works, but when the user zooms out, the image is not at the center of the screen, but at the top.

i've tried something like this

CGPoint scrollCenter = [scroller center];
[img setCenter:CGPointMake(scrollCenter.x, scrollCenter.y)];

or

img.center = scroller.center;

but with this solution, the image is not completly scrollable, and if i zoom out, it stay again at the top of the screen, but is not completly visible!

What can i do for fix it?

like image 241
JAA Avatar asked Apr 29 '11 15:04

JAA


2 Answers

You have to do it manually while the zooming is in progress using the scrollViewDidZoom delegate function... something like

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{

    // center the image as it becomes smaller than the size of the screen
    CGSize boundsSize = scrollView.bounds.size;
    CGRect frameToCenter = imageView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
    {
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    } else {
        frameToCenter.origin.x = 0;
    }

    // center vertically
    if (frameToCenter.size.height < boundsSize.height) 
    {
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    } else {
        frameToCenter.origin.y = 0;
    }

    imageView.frame = frameToCenter;

}
like image 139
Bastian Avatar answered Nov 16 '22 23:11

Bastian


A Swift 3.0 version of Bastian 's answer, a little more succinct:

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    // center the image as it becomes smaller than the size of the screen
    let boundsSize    = scrollView.bounds.size
    var frameToCenter = imageView.frame

    // center horizontally and vertically
    let widthDiff  = boundsSize.width  - frameToCenter.size.width
    let heightDiff = boundsSize.height - frameToCenter.size.height
    frameToCenter.origin.x = (widthDiff  > 0) ? widthDiff  / 2 : 0;
    frameToCenter.origin.y = (heightDiff > 0) ? heightDiff / 2 : 0;

    imageView.frame = frameToCenter;
}
like image 27
Roy Shilkrot Avatar answered Nov 16 '22 22:11

Roy Shilkrot