是否可以像Apple的Photos App一样在UIScrollView中居中显示内容? [英] Is it Possible to Center Content in a UIScrollView Like Apple's Photos App?

查看:96
本文介绍了是否可以像Apple的Photos App一样在UIScrollView中居中显示内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在努力解决这个问题,似乎没有明确的答案。请让我知道是否有人发现了这个问题。

I have been struggling with this problem for some time and there seems to be no clear answer. Please let me know if anyone has figured this out.

我想在 UIScrollView 中显示一张照片在屏幕上居中放置(图像可能是纵向或横向)。

I want to display a photo in a UIScrollView that is centered on the screen (the image may be in portrait or landscape orientation).

我希望能够像在照片应用中那样将图像缩放和平移到图像的边缘。

I want to be able to zoom and pan the image only to the edge of the image as in the photos app.

我尝试更改 viewDidEndZooming 方法中的 contentInset ,这种方法可以解决问题,但是

I've tried altering the contentInset in the viewDidEndZooming method and that sort of does the trick, but there's several glitches.

我也尝试过使 imageView 的大小与 scrollView 并将图像居中。问题在于,缩放时,您可以滚动整个 imageView ,而部分图像可能会滚动离开视图。

I've also tried making the imageView the same size as the scrollView and centering the image there. The problem is that when you zoom, you can scroll around the entire imageView and part of the image may scroll off the view.

我在这里找到了类似的帖子,并给出了最佳解决方法,但未找到真实答案:

I found a similar post here and gave my best workaround, but no real answer was found:

具有居中UIImageView的UIScrollView,例如Photos应用

推荐答案

这是我可以针对此问题提出的最佳解决方案。诀窍是不断重新调整imageView的框架。我发现此方法比不断调整contentInsets或contentOffSets更好。我不得不添加一些额外的代码来容纳纵向和横向图像。

Here is the best solution I could come up with for this problem. The trick is to constantly readjust the imageView's frame. I find this works much better than constantly adjusting the contentInsets or contentOffSets. I had to add a bit of extra code to accommodate both portrait and landscape images.

如果有人可以提出一种更好的方法,我很想听听

If anyone can come up with a better way to do it, I would love to hear it.

这是代码:

- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

CGSize screenSize = [[self view] bounds].size;

if (myScrollView.zoomScale <= initialZoom +0.01) //This resolves a problem with the code not working correctly when zooming all the way out.
{
    imageView.frame = [[self view] bounds];
    [myScrollView setZoomScale:myScrollView.zoomScale +0.01];
}

if (myScrollView.zoomScale > initialZoom)
{
    if (CGImageGetWidth(temporaryImage.CGImage) > CGImageGetHeight(temporaryImage.CGImage)) //If the image is wider than tall, do the following...
    {
            if (screenSize.height >= CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is greater than the zoomed height of the image do the following...
            {
                    imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), 368);
            }
            if (screenSize.height < CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is less than the zoomed height of the image do the following...
            {
                    imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]);
            }
    }
    if (CGImageGetWidth(temporaryImage.CGImage) < CGImageGetHeight(temporaryImage.CGImage)) //If the image is taller than wide, do the following...
    {
            CGFloat portraitHeight;
            if (CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale] < 368)
            { portraitHeight = 368;}
            else {portraitHeight = CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale];}

            if (screenSize.width >= CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is greater than the zoomed width of the image do the following...
            {
                    imageView.frame = CGRectMake(0, 0, 320, portraitHeight);
            }
            if (screenSize.width < CGImageGetWidth (temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is less than the zoomed width of the image do the following...
            {
                    imageView.frame = CGRectMake(0, 0, CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale], portraitHeight);
            }
    }
    [myScrollView setZoomScale:myScrollView.zoomScale -0.01];
}

这是我如何在viewDidLoad方法中计算最小缩放比例。 / p>

Here is how I calculate the the min zoom scale in the viewDidLoad method.

CGSize photoSize = [temporaryImage size];
CGSize screenSize = [[self view] bounds].size;

CGFloat widthRatio = screenSize.width / photoSize.width;
CGFloat heightRatio = screenSize.height / photoSize.height;
initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
[myScrollView setMinimumZoomScale:initialZoom];
[myScrollView setZoomScale:initialZoom];
[myScrollView setMaximumZoomScale:3.0];

这篇关于是否可以像Apple的Photos App一样在UIScrollView中居中显示内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆