开发者

Zoom in UIImageView without scrollView nor WebView

开发者 https://www.devze.com 2022-12-25 15:48 出处:网络
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 ch

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 wi开发者_StackOverflowth "two-fingered" multi-touch

Thanks a lot!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号