Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zoom in/out an UIImage object when user pinches screen?

I would like to zoom in/out an UIImage object when the user performs the standard pinch action on my application. I'm currently using a UIImageView to display my image, if that detail helps in any way.

I'm trying to figure out how to do this, but no such luck so far.

Any clues?

like image 601
jpm Avatar asked Feb 01 '09 02:02

jpm


People also ask

How do you pinch zoom out?

Pinch 2 or more fingers together or apart to adjust zoom. To zoom temporarily, quickly tap the screen 3 times and hold down your finger on the third tap. Drag your finger to move around the screen. Lift your finger to zoom out.

How do you pinch to zoom in?

To use pinch-to-zoom, touch two fingers on the touch screen, and move them apart to zoom in, or together to zoom out.

How do I zoom an image in swift 5?

Try panning and zooming (hold the 'option' key if you're using the simulator) - you'll get a whole new perspective on your image!


1 Answers

As others described, the easiest solution is to put your UIImageView into a UIScrollView. I did this in the Interface Builder .xib file.

In viewDidLoad, set the following variables. Set your controller to be a UIScrollViewDelegate.

- (void)viewDidLoad {     [super viewDidLoad];     self.scrollView.minimumZoomScale = 0.5;     self.scrollView.maximumZoomScale = 6.0;     self.scrollView.contentSize = self.imageView.frame.size;     self.scrollView.delegate = self; } 

You are required to implement the following method to return the imageView you want to zoom.

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {     return self.imageView; } 

In versions prior to iOS9, you may also need to add this empty delegate method:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { } 

The Apple Documentation does a good job of describing how to do this:

like image 97
rogue Avatar answered Sep 19 '22 22:09

rogue