After seeing many pinch zoom samples I am unhappy, most of samples show only how to use UIPinchGestureRecognizer but none of sample tell how to continue apply pinch zoom from where it left. In this sample I tried to support pinch zoom from where it left.
![]() |
| Before Zoom |
![]() |
| After Zoom |
Creating UIPinchGestureRecognizer And adding To Pich View.
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[mGestureView addGestureRecognizer:pinchGesture];
[pinchGesture release];
Heare mGestureView is UIView on which we adding UIPinchGestureRecognizer instance
Handling Pinch Zoom
-(void)handlePinch:(UIPinchGestureRecognizer*)sender {
/*
There are different actions to take for the different states of the gesture recognizer.
* In the Began state, use the pinch location to find the index path of the row with which the pinch is associated, and keep a reference to that in pinchedIndexPath. Then get the current height of that row, and store as the initial pinch height. Finally, update the scale for the pinched row.
* In the Changed state, update the scale for the pinched row (identified by pinchedIndexPath).
* In the Ended or Canceled state, set the pinchedIndexPath property to nil.
*/
NSLog(@"latscale = %f",mLastScale);
mCurrentScale += [sender scale] - mLastScale;
mLastScale = [sender scale];
if (sender.state == UIGestureRecognizerStateEnded)
{
mLastScale = 1.0;
}
CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, mCurrentScale, mCurrentScale);
mGestureView.transform = newTransform;
}
The logic here is simple first we getting current scale value.
mCurrentScale += [sender scale] - mLastScale;
we are minusing it with previos pinch only to get current zoom. And once user lifts finger we are reseting lastscale value to 0.0 (i.e UIPinchGestureRecognizer values start from 1.0 actual zoom ) in UIGestureRecognizerStateEnded.
Lastly we applying CGAffineTransformScale to view to zoom.
CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, mCurrentScale, mCurrentScale);
mGestureView.transform = newTransform;
Note:
One more nice and full coverage tutorial about UIGestureRecognize is return by Raywanderlich
click here to Read Tutorial.
Note:
One more nice and full coverage tutorial about UIGestureRecognize is return by Raywanderlich
click here to Read Tutorial.


Jesus Christ.... remove some of the damn popups...
ReplyDeletepopups are an extreme overkill. really gives this blog a cheap warez site feel. even though the content is good, I think I will try to avoid this blog from now on.
ReplyDeleteI hope that Hell doesn't have popups like these
ReplyDeleteAd Block Plus?
ReplyDeleteMax ONE popup PLEASE!!!!!
ReplyDeletewhat is value of mLasttScale and current scale?
ReplyDeleteDamn pop ups... Took me over 1 minute to finally figure out how to enter your site.
ReplyDeletegood
ReplyDeleteOh my .... This is the last time I enter this site. It reminds me of a virus infected PC popup nightmare!
ReplyDeleteCan i get xcode project of this?
ReplyDeleteSample Xcode project uploaded ... Pop up removed.
ReplyDeleteGreat sample.........
ReplyDeleteSuperb smaple
ReplyDeleteThanks dear :)
Very Nice sample :)
ReplyDeleteNikki
Thank you. it works well.
ReplyDeleteThanks for your article - it helped me a lot.
ReplyDeleteThere's a bug though - you can't add scales, you have to multiply them.
Your code does work somehow, but the zoom is not right in some cases.
The proper way is actually even simpler:
-(void)handlePinch:(UIPinchGestureRecognizer*)sender {
mScale = sender.scale*mLastScale;
if (sender.state == UIGestureRecognizerStateEnded) mLastScale = mScale;
CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, mScale,mScale);
view.transform = newTransform;
}
( and you have to set the mLastScale to 1 somewhere in your init)