简体   繁体   中英

UIImageView in UIScrollView not full-screen and pinch zooming

Most of the sample code on the net assumes your UIScrollView will be full-screen.

I'm trying to create a UIScrollView with just a UIImageView inside it, and just want to be able to pinch zoom as would normally happen. My UIScrollView only fills a part of the screen, so I can't just use autosizing to keep it the right size, as the sample applications seem to do.

The closest I've got is to add this to the UIScrollViewDelegate:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGPoint cOffset = imageScrollView.contentOffset;
float cScale = imageScrollView.contentScaleFactor;

CGRect imageFrame = ptImage.frame;

float scale = scrollView.zoomScale;

imageFrame.size.width *= scale;
imageFrame.size.height *= scale;

ptImage.frame = imageFrame;

scrollView.zoomScale = 1.0;
scrollView.frame = CGRectMake(0.0, 0.0, scrollContainerView.frame.size.width, scrollContainerView.frame.size.height);
scrollView.bounds = CGRectMake(cOffset.x * cScale * scale , cOffset.y * cScale * scale, scrollView.bounds.size.width, scrollView.bounds.size.height);

scrollView.contentSize = imageFrame.size;

}

The only problem here is that the bounds offset (scrollView.bounds = ...) is incorrect.

Surely it's far simpler than I've done here?

Any help?

Try returning your UIImageView in :

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

And remove the code you put in the didZoom method and see if that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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