views:

1010

answers:

1

Right now, when I double tap the UIScrollView I run this code (taken from Apple's own example code):

#define ZOOM_STEP 1.5
float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
[imageScrollView zoomToRect:zoomRect animated:YES];

However, I have set the maximum zoom to 3, but I'd like to figure out how to make a double tap zoom to 1px = 1px, by that I mean, the UIScrollView zooms in to the actual size of the UIImage inside it. But I am not sure how to calculate how big the image in order to use it for zoom scaling?

+1  A: 

I tracked down the sample code you were pulling from. It was the TapToZoom project in the ScrollViewSuite available at http://developer.apple.com/iphone.

I changed the line that calculates the newScale to be the ratio between the size of Scroll View's frame, and the size of the Image View's Image. This has the effect you desire of zooming-in until the image is shown at 1:1 resolution.

Note: I also had make sure the UIImageView named imageView was a class variable. In the original sample code, it was declared locally in the viewDidLoad: method.

The caveat here is that the scroll view and the image have the same x/y ratio. I ony did math on the width.

- (void)tapDetectingImageView:(TapDetectingImageView *)view gotDoubleTapAtPoint:(CGPoint)tapPoint {
    // double tap zooms in on them image to a 1:1 resolution (based on width)
    float newScale =  imageView.image.size.width / imageScrollView.frame.size.width;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [imageScrollView zoomToRect:zoomRect animated:YES];
}
Brad Smith
Thanks, Brad! That totally did it. And like you point out, height/height would be the same as the width/width.
Canada Dev