缩小时 UIImageView 在屏幕上居中

发布于 2024-11-04 10:05:40 字数 1555 浏览 1 评论 0原文

我在 UIScrollView 中有一个 UIImageView 。我希望用户可以缩放和导航图像。

这是我的工作代码:

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

一切正常,但是发生了这种情况:最小缩放是屏幕的高度。 我的图像的宽度大于高度,所以我希望最小缩放是宽度。

如果我写的

scroller.minimumZoomScale = fmin(1.0, scalex);

有效,但是当用户缩小时,图像不在屏幕的中心,而是在顶部。

我已经尝试过类似的方法

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

,但

img.center = scroller.center;

使用此解决方案,图像不完全可滚动,如果我缩小,它会再次停留在屏幕顶部,但不完全可见!

我能做什么来修复它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-11-11 10:05:40

您必须在使用scrollViewDidZoom委托函数进行缩放时手动执行此操作...类似

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

}

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;

}
旧情勿念 2024-11-11 10:05:40

Bastian 的答案的 Swift 3.0 版本更简洁一些:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文