xcode 在 UIScrollView 中更改内容的中心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8045677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing centering of content within UIScrollView
提问by master_gracey
I have a UIScrollView with an UIImageView as the content. It displays fine, and scrolls fine. No problems there. The image is considerably wider than the screen, so I want to be able to "auto center" a specific portion (coordinates)of the image within the scrollview.
我有一个 UIScrollView 与 UIImageView 作为内容。它显示良好,滚动良好。没有问题。图像比屏幕宽得多,所以我希望能够在滚动视图中“自动居中”图像的特定部分(坐标)。
Here is where I'm stuck, though. The desired centering location will dynamically change based on the use of the application, so I need to be able to configure it so that a desired coordinate of the image view (x,y) is centered automatically within the scrollview when loaded. It doesn't have to autoscroll/animate, it just needs to be there.
不过,这就是我卡住的地方。所需的居中位置将根据应用程序的使用动态更改,因此我需要能够对其进行配置,以便图像视图 (x,y) 的所需坐标在加载时自动在滚动视图内居中。它不必自动滚动/动画,它只需要在那里。
I've seen similar questions/answers for centering content when gestures/zooming are being used, but neither are employed here. Just scrolling. I just need to be able to have a set portion of the image be centered within the scrollview when it first loads.
当使用手势/缩放时,我已经看到了类似的关于将内容居中的问题/答案,但这里都没有使用。只是滚动。我只需要能够在第一次加载时将图像的一组设置在滚动视图中居中。
I hope I've made sense. Thanks for your time.
我希望我说得有道理。谢谢你的时间。
回答by CodaFi
I'm on an iPhone right now, so excuse me if this code is a little funky, as I can't remember exactly if the center property is available without calling size.center first...
我现在在 iPhone 上,所以请原谅我,如果这段代码有点古怪,因为我不记得如果不先调用 size.center 就可以使用 center 属性...
CGPoint centerOffset = CGPointMake(0, [scrollView contentSize].(size).center);
[scrollView setContentOffset: centerOffset animated: YES]; // (or No, depends on you)
回答by Yariv Nissim
After a few days of researching this issue I came up with a simple solution using the ScrollView property - contentInset. This will always center the image, even while zooming (if you follow all instructions).
在研究这个问题几天后,我想出了一个使用 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);
}
Call this method in the ScrollView delegate:
在 ScrollView 委托中调用此方法:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
[self centerImage];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
[self centerImage];
}
AND when you layout your SubViews:
并且当您布局子视图时:
- (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];
}
回答by Matt Connolly
Simply setting its position in the scrollview works. For example:
只需在滚动视图中设置其位置即可。例如:
- (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);
}
And call that in your initialisation and also from - (void)scrollViewDidZoom:(UIScrollView *)scrollView
if you are zooming.
并在您的初始化和- (void)scrollViewDidZoom:(UIScrollView *)scrollView
缩放时调用它。
回答by Josh Sherick
Not sure if this is what you are looking for but I just thought I would put it out there, does this method help?
不确定这是否是您要找的东西,但我只是想我会把它放在那里,这种方法有帮助吗?
- (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;
}
Or this one?
还是这个?
- (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;
}
Note: I didn't write these, they are from sample code provided by Apple but I use them occasionally and I thought of them when I saw your question.
注意:这些不是我写的,它们来自Apple提供的示例代码,但我偶尔会使用它们,当我看到你的问题时我想到了它们。