iosobjective-czoominguipinchgesturerecognizer

Specfic part of UIView zoom in/out without increasing hight/width of View objective C iOS


I implement this .but problem is that it increase hight/width of the view with zooming.How to zoom without increasing the hight/width. My UIView hight/width is fixed.My basic aim is that view hight/width remain same but zoom inner side of the view.

    UIPinchGestureRecognizer * pinGeture =  [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(twoFingerPinch:)];
    [radarViewHolder addGestureRecognizer:pinGeture];



- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    NSLog(@"Pinch scale: %f", recognizer.scale);

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

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

        CGFloat currentScale = [[[recognizer 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 - [recognizer scale]);
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        CGAffineTransform transform = CGAffineTransformScale([[recognizer view] transform], newScale, newScale);
        [recognizer view].transform = transform;

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

Solution

  • @ShahbazAkram declare one float variable CGFloat lastScale;

    replace this method into your code.

    - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
    {
        NSLog(@"Pinch scale: %f", recognizer.scale);
    
        if([recognizer state] == UIGestureRecognizerStateBegan) {
            // Reset the last scale, necessary if there are multiple objects with different scales
            lastScale = [recognizer scale];
        }
    
        if ([recognizer state] == UIGestureRecognizerStateBegan ||
            [recognizer state] == UIGestureRecognizerStateChanged) {
    
            CGFloat currentScale = [[[recognizer 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 - [recognizer scale]);
            newScale = MIN(newScale, kMaxScale / currentScale);
            newScale = MAX(newScale, kMinScale / currentScale);
            CGAffineTransform transform = CGAffineTransformScale([[recognizer view] transform], newScale, newScale);
            [recognizer view].transform = transform;
    
            lastScale = [recognizer scale];  // Store the previous scale factor for the next pinch gesture call
        }
    }