我在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;
}
有什么建议吗?
答案 0 :(得分:0)
我认为this就是你想要的。
答案 1 :(得分:0)
如果不需要分页,则禁用分页,它可以正常工作
scrollview.pagingEnabled = NO;
答案 2 :(得分:-1)
下面是我在我的应用程序中使用的代码(改编自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;