简体   繁体   English

缩小时在屏幕上居中 UIImageView

[英]Center an UIImageView on the screen when zoom out

I have an UIImageView inside an UIScrollView.我在 UIScrollView 中有一个 UIImageView。 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?我能做些什么来修复它?

You have to do it manually while the zooming is in progress using the scrollViewDidZoom delegate function... something like您必须在使用 scrollViewDidZoom 委托 function 进行缩放时手动执行它......类似于

- (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;

}

A Swift 3.0 version of Bastian 's answer, a little more succinct:一个 Swift 3.0 版本的 Bastian 的回答,更简洁一点:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM