简体   繁体   English

iPhone-在iPad上的UIScrollView中无法缩放/图像放置

[英]iPhone - Trouble zooming/image placement in UIScrollView on iPad

I am having trouble with my ScrollView. 我的ScrollView遇到问题。 I am programmatically adding a subview UIImageView to the ScrollView, and the implementing zoom. 我以编程方式将子视图UIImageView添加到ScrollView,并实现缩放。 I was having no trouble on the iPhone using the code below to center the image, but on the iPad the image is off to the right and shoved down. 使用以下代码将图像居中,我在iPhone上没有遇到问题,但在iPad上,图像偏向右侧并向下推。 I added the storyboard for iPad and an empty view controller and hooked it up, just like I did for iPhone. 我添加了适用于iPad的情节提要和一个空视图控制器,并将其连接起来,就像使用iPhone一样。 I don't have a ScrollView subclass (so simple, I did not think it was needed). 我没有ScrollView子类(如此简单,我认为它不是必需的)。

How do I get the image/scrollview to be centered on the iPad? 如何使图像/滚动视图在iPad上居中?

Code: I don't specify location or size anywhere else in the code 代码:我没有在代码中的其他任何地方指定位置或大小

This is all in my viewcontroller.m 这一切都在我的viewcontroller.m中

//Create scrollView.
    _scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
    [_scrollView setBackgroundColor:[UIColor whiteColor]];
    [_scrollView setDelegate:self];
    [_scrollView setBouncesZoom:YES];
    [[self view] addSubview:_scrollView];

//Add violinImage to scrollView
    _currentImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_currentImageName]];
    [_scrollView setContentSize:[_currentImage frame].size];
    [_scrollView addSubview:_currentImage];

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _myImage;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    UIView *subView = [scrollView.subviews objectAtIndex:0];
    CGFloat offsetX;
    CGFloat offsetY;
    if (IS_IPAD)
    {
        offsetX = (scrollView.bounds.size.width - scrollView.contentSize.width) /2;
        offsetY = scrollView.bounds.size.height * 0.3;
    }
    else
    {
        if (scrollView.bounds.size.width > scrollView.contentSize.width) {
            offsetX = (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5;
        }
        else
        {
            offsetX = 0.0;
        }

        if (scrollView.bounds.size.height > scrollView.contentSize.height)
        {
            offsetY = (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5;
        }
        else
        {
            offsetY = 0.0;
        }
    }
    subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY);
}

Thank you! 谢谢!

So this is sort of a unique case for me, but on the iPad, my image was not larger than the scrollview frame after zoom. 因此,这对我来说是一种特殊的情况,但是在iPad上,我的图像不大于缩放后的滚动视图框架。 A part of code I did not show in my original question because I did not think it was relevant. 我在原始问题中未显示的一部分代码,因为我认为它不相关。 Whoops. 哎呀

It worked on the iPhone, because my image was always larger than the frame, so it did not apply any offset, and centered the scrollview (and contents). 它可以在iPhone上使用,因为我的图像始终大于框架,因此它不应用任何偏移,并且使滚动视图(和内容)居中。 On the iPad I was getting an offset value with the iPhone math, so I needed to reverse the offset calculation. 在iPad上,我通过iPhone数学获得了偏移值,因此我需要反转偏移量计算。

Here is what I did when setting the zoom to get it to work. 这是设置缩放使其正常工作时所做的事情。

if (IS_IPAD && IS_RETINA) {
        float topInset = (imageSize.height - maxSize.height) / 2.0;
        float sideInset = (imageSize.width - maxSize.width) / 2.0;
        if (topInset < 0.0) topInset = 0.0;
        if (sideInset < 0.0) sideInset = 0.0;
        [_scrollView setContentInset:UIEdgeInsetsMake(topInset, sideInset, -topInset, -sideInset)];
    }
    else
    {
        float topInset = (maxSize.height - imageSize.height) / 2.0;
        float sideInset = (maxSize.width - imageSize.width) / 2.0;
        if (topInset < 0.0) topInset = 0.0;
        if (sideInset < 0.0) sideInset = 0.0;
        [_scrollView setContentInset:UIEdgeInsetsMake(topInset, sideInset, -topInset, -sideInset)];
    }

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

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