views:

481

answers:

1

Hi, Is there any code example teaching how to zoom in and out in a UIImageView by user taps?

I know it is possible to do it with UIScrollView and with UIWebView, but these solutions both need a lot of changes in my code, and I'm working on schedule due to college project deadlines.

I want basically an example on how to manipulate directly the UIImageView, having the same behavior as the photo album in iPhone:

  • first double touch -> zoom in
  • second double touch -> zoom out
  • pinch and zoom with "two-fingered" multi-touch

Thanks a lot!

A: 

I'm not sure that rolling your own solution is going to be simpler than UIScrollView or UIWebView. If you have your heart set on rolling your own solution, however, it can be done with custom event handling and changing the frame of the UIImageView. You will need to process touch events on the UIImageView (or its container) by overriding the following methods:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

You will need to distinguish between taps and pinches in these methods and set the frame of the UIImageView accordingly. Remember that frame is an animatable property of UIView, so you can also animate these changes.

If you don't already have an idea of how you're going to process events in these methods, then I strongly suggest that you reconsider using UIScrollView or UIWebView to do what you want.

glorifiedHacker
Hi. I've been looking at some code, and I'm starting to think it's probably for the best to re-write my view completely. But it's a bummer, nevertheless! :/
camilo
Hi accepted this answer because it was correct. Nevertheless, I went with UIScrollView... after I tried UIWebView. Basically I've done it all, except the way I wanted at first. :P
camilo
I've been there - sometimes only discovering after reaching the end that I should have done it another way!
glorifiedHacker