Limiting Zoom level in UIPinchGestureRecognizer

Sometimes when we are zooming image by using Pinch Gesture, we can’t handle the size of zooming. Here is a small code to do this.

The code below assumes there is an instance variable CGFloat lastScale and that a view has been set for the UIPinchGestureRecognizer.

– (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {

CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@”transform.scale”] floatValue];

// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;

CGFloat newScale = 1 –  (lastScale – [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;

lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call

!! Enjoy !!

This entry was posted in iOS by Muzammil. Bookmark the permalink.

About Muzammil

Hey Guys, I am Muzammil Mohammad. Having 7 years of experience in iOS development. I am here to share my knowledge with all mobile application developers and those companies who are stuck on development (due to bad quality of code or don't know what to do) or want to know how to build a good mobile app. I believe I am more expert in UI and Architecture design of mobile app. So if you need my helps or assistant for your mobile app projects, Feel free to contact me. I will try my best to improve the quality of your app. Or you can hire me to develop your apps. I’d love to talk with you. Best!

6 thoughts on “Limiting Zoom level in UIPinchGestureRecognizer

  1. I just learned that [[self alloc] init ] trick about a week ago. Not only is it more rallebie, but it results in tighter code.I think the size and position properties are solid wins, but I’m less convinced about adding an incomplete subview to a parent view. Won’t that cause extra update events as you set up the view? If so the only thing I’d add to this is a viewWithFrame.

    • Thanks for the category! I waentd to write it myself because I’ve really missed those useful methods when I switched form Qt to Cocoa. But then I decided to do a search and here it is already done for me.Thanks a lot

  2. Perhaps you were inspired by WWDC 2009 sessoin 128 (prototyping for iPhone) I’ve been looking for that particular code for a while with no luck, and the presenter didn’t scroll through the whole .m file in his demo. However, what you put together works perfectly and I was able to go through the whole prototyping process easily because of this post thanks!!

    • This is great! I spend too much time working on long coutsm scripts for commercial projects. I love seeing how efficiently some effects can be implemented. This little engine’ is a fun starting point for a weekend play project. Thanks Seb!- James

Leave a Reply

Your email address will not be published. Required fields are marked *