Saturday, November 26, 2011

Zooming UIImageView in iOS 5.0

Create a new project using xcode 4.0 with runtime iOS 5.0.

Suppose you have created view based project.

In root view controller class.

Add UIScrollView in xib and connect outlet to root ViewController.
Add UIImageView as subview on xib and connect its outlet to root ViewController OR create it writing code in root ViewController class and add subView to ScrollView.

Add UITapGestureRecognizer to UIImageView instance for single double taps and two finger touches.

set the UIScrollView's scale

// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [scrollView frame].size.width / [imageView frame].size.width;
scrollView.maximumZoomScale = 1.0;
scrollView.minimumZoomScale = minimumScale;
scrollView.zoomScale = minimumScale;

//note set the imageView's frame in ViewwillAppear or ViewDidAppear.
imageView.frame = CGRectMake(0,0, 320, 480);

when tap occurs at imageView, calculate the rect and set the rect to scroll view.
[scrollView zoomToRect:zoomRect animated:YES];


here is the sample code

No comments:

Post a Comment