更改UIScrollView中的内容中心

时间:2011-11-08 03:42:00

标签: ios xcode uiscrollview uiimageview

我有一个UIScrollView,其中包含UIImageView作为内容。它显示很好,滚动很好。没有问题。图像比屏幕宽得多,所以我希望能够在滚动视图中“自动居中”图像的特定部分(坐标)

但这是我被困的地方。所需的居中位置将根据应用程序的使用情况动态变化,因此我需要能够对其进行配置,以便在加载时图像视图(x,y)的所需坐标在滚动视图内自动居中。它不需要自动滚动/动画,它只需要在那里。

我在使用手势/缩放时看到过类似内容的问题/答案,但这里都没有使用。只是滚动。我只需要能够在首次加载时将图像的一部分集中在scrollview中。

我希望我有道理。谢谢你的时间。

4 个答案:

答案 0 :(得分:5)

我现在正在使用iPhone,如果这段代码有点时髦,请原谅我,因为我不记得中心属性是否可用而没有调用size.center首先......

CGPoint centerOffset = CGPointMake(0, [scrollView contentSize].(size).center);
[scrollView setContentOffset: centerOffset animated: YES]; // (or No, depends on you)

答案 1 :(得分:2)

经过几天研究这个问题后,我想出了一个使用ScrollView属性的简单解决方案 - contentInset。 这将始终使图像居中,即使在缩放时(如果您遵循所有说明)。

- (void)centerImage
{
    if (!self.image) return;

    CGFloat imageScaleWidth = self.image.size.width * self.scrollView.zoomScale;
    CGFloat imageScaleHeight = self.image.size.height * self.scrollView.zoomScale;

    CGFloat hOffset = (self.frame.size.width - imageScaleWidth) * 0.5f;
    CGFloat vOffset = (self.frame.size.height - imageScaleHeight) * 0.5f;

    if (hOffset < 0) hOffset = 0;
    if (vOffset < 0) vOffset = 0;

    self.scrollView.contentInset = UIEdgeInsetsMake(vOffset, hOffset, 0, 0);
}

在ScrollView委托中调用此方法:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    [self centerImage];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    [self centerImage];
}

当您布置SubView时:和

- (void)layoutScrollView
{
    if (!self.image) return;

    CGFloat heightScale = self.frame.size.height / self.image.size.height;
    CGFloat widthScale = self.frame.size.width / self.image.size.width;
    CGFloat scale = MIN(widthScale, heightScale);

    self.scrollView.minimumZoomScale = scale;
    self.scrollView.maximumZoomScale = MAX(1.0f ,scale * 2.0f);
    [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:NO];

    [self centerImage];
}

答案 2 :(得分:1)

只需在scrollview中设置其位置即可。例如:

- (void)centreImageView
{
    CGRect bounds = self.bounds;
    CGSize contentSize = self.contentSize;
    CGFloat offsetX = MAX(0, (bounds.size.width - contentSize.width) * 0.5f);
    CGFloat offsetY = MAX(0, (bounds.size.height - contentSize.height) * 0.5f);

    _myContentView.center = CGPointMake(contentSize.width * 0.5 + offsetX, contentSize.height * 0.5 + offsetY);
}

如果你正在缩放,请在初始化时调用它,也可以从- (void)scrollViewDidZoom:(UIScrollView *)scrollView调用。

答案 3 :(得分:0)

不确定这是否是您正在寻找的但我只是认为我会把它放在那里,这种方法有帮助吗?

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [scroller frame].size.height / scale;
    zoomRect.size.width  = [scroller frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}

还是这个?

- (CGRect)zoomRectForScale:(float)scale withOrigin:(CGPoint)origin {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [scroller frame].size.height / scale;
    zoomRect.size.width  = [scroller frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = origin.x;
    zoomRect.origin.y    = origin.y;

    return zoomRect;
}

注意:我没有写这些,它们来自Apple提供的示例代码,但我偶尔使用它们,当我看到你的问题时我就想到了它们。