UIPinchGestureRecognizer for zooming and panning an image in xcode

前端 未结 2 1726
Happy的楠姐
Happy的楠姐 2021-01-22 18:33

I have created one application in which there is one view containing an image view. Below code for zooming an image using UIPinchGestureRecognizer is working. Here innerview is

2条回答
  •  不知归路
    2021-01-22 19:19

    Embed your innerView in Scroll View Once you are done with pinching, you can get size of new image and then set its content size

    -(void)scale:(id)sender {
      if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        lastScale = 1.0;
        CGSize zoomViewSize = CGSizeApplyAffineTransform(imgScrollView.frame.size, [(UIPinchGestureRecognizer *)sender view].transform);
        CGPoint centerPoint = CGPointApplyAffineTransform(imgScrollView.frame.origin, [(UIPinchGestureRecognizer *)sender view].transform);
    
        CGRect r = imgScrollView.bounds;
        CGSize scrollViewSize = imgScrollView.frame.size;
        imgScrollView.contentSize = zoomViewSize;
        imgScrollView.contentOffset = centerPoint; 
      }
    
      CGFloat currentScale = [[[(UIPinchGestureRecognizer*)sender view].layer valueForKeyPath:@"transform.scale"] floatValue];
    
      CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
      scale = MIN(scale, imgScrollView.maximumZoomScale / currentScale);   
      scale = MAX(scale, imgScrollView.minimumZoomScale / currentScale);
      CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
      CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
      [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
      lastScale = [(UIPinchGestureRecognizer*)sender scale];    
    }
    

    Hope this help you :)

提交回复
热议问题