Kevin Renskers
Kevin Renskers

Reputation: 5940

How to pan a zoomed out UIImageView inside an UIScrollView?

I have an UIScrollView which contains one UIImageView. Everything is working correctly, I can zoom in and out, when zoomed in I can pan around, etc.

The problem that I am unable to solve is how to pan a zoomed out image around the screen. The user needs to be able to zoom the image out until it's small, and then move that small image to any point on the screen. Sadly, it's stuck in the center of the screen (usually, it would be stuck in the top left corner but I did fix that problem).

Upvotes: 0

Views: 532

Answers (1)

Live2Enjoy7
Live2Enjoy7

Reputation: 1105

There are a couple of things you need to do here. I suggest doing them in viewWillAppear rather than viewDidLoad because if you're using storyboards, it doesn't work quite right in viewDidLoad.

First, you need to set the content size property of your scroll view to be the size of the photo itself because you want the entire area of your photo to be scrollable.

self.scrollView.contentSize=self.photoShown.size; 

photoShown is UIImage. scrollView is a UIScrollView

Second, you need to set the frame of your UIImageView that houses your image to be the size of your image:

self.imageView.frame=CGRectMake(0,0, self.photoShown.size.width, self.photoShown.size.height);

if you haven't done this, then the frame of the UIImageView is the size of the screen itself and there is simply nowhere to pan. That's why it needs to be bigger, so you can pan to the regions not currently shown on screen.

Upvotes: 2

Related Questions