UIPinchGestureRecognizer for zooming and panning an image in xcode

前端 未结 2 1727
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 :)

    0 讨论(0)
  • 2021-01-22 19:27

    try this

     -(void)scale:(id)sender {
    
    
        if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
            _lastScale = 1.0;
        }
    
        CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);
        CGAffineTransform currentTransform = self.transform;
        CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    
        [self setTransform:newTransform];
    
    
    }
    
    0 讨论(0)
提交回复
热议问题