Resetting zoom with UIScrollView

Posted: February 6, 2009 in iphone-dev
Tags: ,

Update 6/2010: I wrote this post prior to the release of iPhone SDK 3.0. See my comment for a straight forward approach in 3.0+.

This is my first blog post about something along my iPhone development travels that had me stumped.  I’ve had quite a few, having come from a primarily Java background, so I hope to post more.

I’ve only been working with Objective-C and Cocoa Touch for about 4-5 months now, so it’s very likely there’s an easier way to do some of this.  Feel free to post comments but don’t knock a n00b too badly.

I have an app that displays photos, nearly full-screen.  I wanted to add zooming, so I looked it up in the SDK docs and adding touch-zoom was a cinch.

Adding zoom reset wasn’t so straightforward.  Nor was it even documented in the SDK docs.

It turned out (in my case) to be fairly simple, but took a lot of trial and error.  I don’t know why Apple can’t just add a

-(void)resetZoom:(BOOL)animated

method to UIImageView.  It makes me wonder if perhaps the approach I took won’t work for all scenarios so take this as my “your mileage may vary” disclaimer.

For my app(s), the only subview of of the UIScrollView is a single UIImageView (I have other view components omitted here for brevity):

+---UIView (superview)
¦   +---UIScrollView (myScrollView)
¦   ¦   +---UIImageView (myImageView)

Here is what I did in my view controller managing the view containing the scroll view:

Summary:
1) I store the original “center” point of the scrollview subview that is being zoomed by snagging it in viewDidLoad. *
2) I created a Transform via the CGAffineTransformMakeScale() function, specifying 1.0 as the scale dimensions and apply it to the imageview.
3) I set the content size of the scroll view to zero to stop it from scrolling. **
4) I set the center of the image view back to its original center.

Notes:
* my views are all prototyped in Interface Builder, and I didn’t want to have to hard code this number.  If an app was building the UI programmatically keeping track of this would be a bit different.
** This seemed odd to me at first; my gut tells me this works for me because most of my views are set to auto resize to fill; I have not experimented outside this setup.

Code:

- (void)viewDidLoad {
    NSLog(@"VC view did load");
    [super viewDidLoad];
    // ... other work
    originalImagePos = myImageView.center;
}

//...

-(void)resetImageZoom {
    NSLog(@"Resetting any image zoom");
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0, 1.0);
    myImageView.transform = transform;
    [myScrollView setContentSize:CGSizeZero];
    myImageView.center = originalImagePos;
}

I found the tip about using CAAffineTransform online, but that only “unzoomed” my image view; the scroll view was still scrollable – the image view could be scrolled completely out of the viewport!  I spent the rest of my time trying to figure out how to fix that and through trial and error found that repositioning the scrollview’s subview and setting the scrollview’s content size to zero worked for me.

Since I couldn’t find any blogs or forum posts about stuff other than the CAAffineTransform step at the time, I’ve posted this to hopefully help anyone else having this same problem.  I had one person follow up on my tweet for help who had the same issue and the samples above are largely from an e-mail response I sent to him.

Happy iPhoning,

-Scott H

Advertisement
Comments
  1. Jerry says:

    Thanks for this! Helped me a lot.

  2. Well. This got wayyy easier in OS 3.0, and I’m just now getting around to noticing. I was able to ditch the originalImagePos property (which didn’t work with rotation, by the way).

    -(void)resetImageZoom {
        NSLog(@"Resetting any image zoom");
        [myScrollView setZoomScale:1.0 animated:YES];
    }
    

    Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s