2

I created a custom UIView and I am trying to have UIPinchGestureRecognizer handle the resize of it. But when I test my code the text is being resized when pinch and the frame is still the same size.

in floatingToolbar.m

-(void) pinchFired:(UIPinchGestureRecognizer *)recognizer{
    if (recognizer.state == UIGestureRecognizerStateChanged) {

        CGFloat scale = [recognizer scale];

        if ([self.delegate respondsToSelector:@selector(floatingToolbar:didPinchWithScale:)]) {
            [self.delegate floatingToolbar:self didPinchWithScale:scale];
        }

        [recognizer setScale:scale];
    }
}

in SBViewController.m

-(void) floatingToolbar:(SBAwesomeFloatingToolbar *)toolbar didPinchWithScale:(CGFloat)scale{
    NSLog(@"Scale is ------  %f",scale);   
    CGAffineTransform currentTransform = CGAffineTransformIdentity;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    toolbar.transform = newTransform;
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
sfbarry14
  • 61
  • 5

1 Answers1

3

There are basically two issues:

Updating the Scale

First, I think you want to change [recognizer setScale:scale] to recognizer.scale = 1. Otherwise the size will change exponentially instead of linearly as the user continues to pinch.

For example, with .scale = 1, your log will look something like:

Log                       Transform Scale

                          1
Scale is ------  0.98     0.98
Scale is ------  0.99     0.97
Scale is ------  0.98     0.95
Scale is ------  0.98     0.93

With your current approach (which doesn't change the scale at all), your log will look like:

Log                       Transform Scale

                          1
Scale is ------  0.98     0.98
Scale is ------  0.92     0.90
Scale is ------  0.88     0.79
Scale is ------  0.83     0.65

This is bad because your view will shrink way too fast.

Use of CGAffineTransformScale

CGAffineTransformScale (and the other CGAffineTransforms) don't update the frame.

From the UIView class reference on the transform property:

WARNING: If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored.

If you're relying on frame for your toolbar, you could just modify the toolbar's frame by multiplying scale against its height and width.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287