2

I have a UIViewController which is calling a UIView Class, and I'm using CGAffine Transform to zoom into my UIView class instance using the following code

CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale,    recognizer.scale);
NSLog(@"Pinch scale: %f", recognizer.scale);
float scale = recognizer.scale;
float SCALE_MIN = 1.0f;
float SCALE_MAX = 3.0f;
. . . 

This Basic code, works! (some code has been omitted)

The problem now is regardless of where on the screen I "pinch" the file always zooms from the middle of the file, not at the location as where I am pinching . .

Any suggestions on how I can get it to zoom into the exact location pinched ?

Taskinul Haque
  • 724
  • 2
  • 16
  • 36
  • for those who are interested Found a great solution to smooth zooming : @Paul Solts Answer - http://stackoverflow.com/questions/5150642/max-min-scale-of-pinch-zoom-in-uipinchgesturerecognizer-iphone-ios Also a similar discussion thread : http://stackoverflow.com/questions/4947148/uipinchgesturerecognizer-position-the-pinched-view-between-the-two-fingers – Taskinul Haque Aug 22 '13 at 13:01

3 Answers3

1

The locationInView: method, which is available on all UIGestureRecognizers, when called on an instance of UIPinchGestureRecognizer, will give you the midpoint between the two touches that make up the pinch. That's the closest thing I think you'll get to "the exact location pinched."

When the gesture is first recognized, save the locationInView to get the point at which you want to zoom. I wouldn't try to update the zoom center during the course of the pinch, because I think you'll end up with a complicated implementation that doesn't behave intuitively. You'll want the zoom to keep that center point fixed, and you should be able to do that by setting the anchorPoint of the layer of the view that you're transforming. Note that anchorPoint coordinates are normalized---the default is (0.5, 0.5), the center of the view.

Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
0
- (void)pinchDetected:(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.2;
    const CGFloat kMinScale = 0.64;

    CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]);
    newScale = MIN(newScale, kMaxScale / currentScale);
    newScale = MAX(newScale, kMinScale / currentScale);
    CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
    [gestureRecognizer view].transform = transform;

    [gestureRecognizer setScale:1.0];

    lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call
}
}
iPhone 7
  • 1,731
  • 1
  • 27
  • 63
-1

hope this will helpful for you it's work for me like a charm...

- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
    CGAffineTransform transform = CGAffineTransformScale(pinchRecognizer.view.transform, pinchRecognizer.scale,pinchRecognizer.scale);
    _designImage.transform = transform;
    _Zoomscale = pinchRecognizer.scale;

    NSLog(@"image Pinch scale: %2ff", _Zoomscale);
}
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Ashok Kumar
  • 225
  • 4
  • 9