UIScrollView中的iPhone UIImageView没有缩放到中心
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIScrollView中的iPhone UIImageView没有缩放到中心相关的知识,希望对你有一定的参考价值。
我在UIImageView
里面有一个UIScrollView
所以我可以允许缩放,缩放工作,但它不会保持UIImageView
居中,最终切断图像的底部,并在顶部留下黑色空间。
这就是我所拥有的:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
scroller.bouncesZoom = YES;
scroller.clipsToBounds = YES;
float scale = image.size.width/320;
UIImage *scaled = [UIImage imageWithCGImage:[image CGImage] scale:scale orientation:UIImageOrientationUp];
imageView = [[UIImageView alloc] initWithImage:scaled];
imageView.userInteractionEnabled = YES;
[imageView setCenter:CGPointMake(160,240)];
[scroller addSubview:imageView];
scroller.contentSize = [imageView frame].size;
scroller.maximumZoomScale = 4.0;
scroller.minimumZoomScale = 1.0;
scroller.zoomScale = 1.0;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
有什么建议?
答案
我认为this是你想要的。
另一答案
如果不需要分页,则禁用分页,它可以正常工作
scrollview.pagingEnabled = NO;
另一答案
下面是我在我的应用程序中使用的代码(改编自WWDC示例)也许你可以拿出一些东西:)
CGSize imageSize =[image size];
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;
CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
CGFloat minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible
// on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
// maximum zoom scale to 0.5.
CGFloat maxScale = 1.0 / [[UIScreen mainScreen] scale];
// don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
if (minScale > maxScale) {
minScale = maxScale;
}
scrollview.contentSize=imageSize;
scrollview.maximumZoomScale=maxScale;
scrollview.minimumZoomScale=minScale;
scrollview.zoomScale=1;
以上是关于UIScrollView中的iPhone UIImageView没有缩放到中心的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView中的iPhone UIImageView没有缩放到中心
UIScrollview 宽度不能与 iPhone 大小相等
iPhone OS3 更改为 UIScrollView 子类
如何访问添加到 UIScrollView 的 UIView 上的控件