0

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
    }
}
Shahbaz Akram
  • 1,598
  • 3
  • 28
  • 45

3 Answers3

2

Add your view in yourScroll as subView.

[yourScroll addSubView:radarViewHolder];


- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{

    CGFloat size = scrollView.zoomScale;
    if (size > ZoomMaxValue)
    {
        yourScroll.zoomScale = maximumScale;
    }
    if (size > ZoomMinValue && size < ZoomValue && size < 1.01)
    {
        yourScroll.zoomScale = minimumScale;
    }
    ZoomValue = size;
}
vipinsaini0
  • 541
  • 1
  • 7
  • 26
1

@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
    }
}
Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
0

When using transform on a view then getter for its frame will multiply its coordinates with this transform. So internally the frame was not really updated.

This is usually not a problem but can be in some cases. In general to avoid such cases all you need to do is add your view to another view and then scale the superview. Assume such code:

let baseView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
baseView.backgroundColor = .red
let addedView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
addedView.backgroundColor = .green
baseView.addSubview(addedView)
baseView.transform = CGAffineTransform(scaleX: 3.0, y: 3.0)

(Sorry for Swift but it should make no difference for your case)

When printing frames for these 2 views the result is

baseView: (-50 -50; 150 150)
addedView: (0 0; 50 50)

but visually these 2 views are the same and added view overlaps the base view completely.

In your case there is not enough code to show specific fix but if you create a base view for the view you are currently having and scale the base view then your issue with changed frame should be fixed.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43