缩放滚动视图的特定区域

Zoom a specific area of a Scrollview

我遇到了这样一种情况,我想以编程方式而不是通过触摸或手势来缩放滚动视图的一部分(矩形)。

CGRect cropRect = CGRectMake(xPos * [UIScreen mainScreen].nativeScale, yPos * [UIScreen mainScreen].nativeScale, width1 * [UIScreen mainScreen].nativeScale, height1 * [UIScreen mainScreen].nativeScale);
[self.scrlVPhoto zoomToRect:cropRect animated:YES];

我设置了委托并实现了委托方法。

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)aScrollView {
    return self.containerView;
}

self.containerView 是我要缩放的视图。

我已经尝试了所有方法,但我仍然对如何摆脱它感到困惑。

请有人帮助我。感谢任何帮助。

提前致谢。

您应该在滚动视图中添加要缩放的视图。在委托方法中 return 您要缩放的视图。

我已经创建了 UIImageView,在双击或单击 UIImageView 时在 imageView 中设置缩放选项,代码如下,

viewDidLoad 方法:

   UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

    [doubleTap setNumberOfTapsRequired:2];

双标签法:

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    // zoom in
    float newScale = [myscrollview zoomScale] * 2;

    if (newScale > self.myscrollview.maximumZoomScale){
        newScale = self.myscrollview.minimumZoomScale;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

        [myscrollview zoomToRect:zoomRect animated:YES];

    }
    else{

        newScale = self.myscrollview.maximumZoomScale;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];

        [myscrollview zoomToRect:zoomRect animated:YES];
    }
}


    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

        CGRect zoomRect;

        // the zoom rect is in the content view's coordinates.
        //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
        //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
        zoomRect.size.height = [myscrollview frame].size.height / scale;
        zoomRect.size.width  = [myscrollview frame].size.width  / scale;

        // choose an origin so as to get the right center.
        zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
        zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

        return zoomRect;
    }

你需要的委托方法如下所示,

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if (vmov==nil)
    {

    float newwidth;
    float newheight;

    UIImage *image=detaimageview.image;

    if (image.size.height>=image.size.width){
        newheight=detaimageview.frame.size.height;
        newwidth=(image.size.width/image.size.height)*newheight;

        if(newwidth>detaimageview.frame.size.width){
            float diff=detaimageview.frame.size.width-newwidth;
            newheight=newheight+diff/newheight*newheight;
            newwidth=detaimageview.frame.size.width;

        }

    }
    else{
        newwidth=detaimageview.frame.size.width;
        newheight=(image.size.height/image.size.width)*newwidth;

        if(newheight>detaimageview.frame.size.height){
            float diff=detaimageview.frame.size.height-newheight;
            newwidth=newwidth+diff/newwidth*newwidth;
            newheight=detaimageview.frame.size.height;

            myscrollview.clipsToBounds = NO;

        }
    }
    CGRect frame;
    CGFloat screenWidth;
    CGFloat screenHeight;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    screenWidth = screenRect.size.width;
    screenHeight = screenRect.size.height-64;
    frame.size.width = newwidth;
    frame.size.height = newheight;
    self.myscrollview.contentSize = frame.size;
    float x,y;
    x=0;
    y=0;
    if (newheight<screenHeight)
    {
        x =screenHeight/2-newheight/2;
    }
    if (newwidth<screenWidth)
    {
        y =screenWidth/2-newwidth/2;
    }
    self.detaimageview.frame = CGRectMake(y,x,newwidth,newheight);

    return self.detaimageview;
    }

    return nil;
}

以上代码是缩放 imageView 及其在我的项目中的工作,

希望对您有所帮助

请检查这个。可能对你有帮助

 -(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{

      //[self.superImage_View setAlpha:0.5];

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

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



         CGFloat currentScale = [[[pinchGestureRecognizer 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-[pinchGestureRecognizer scale]) ; // new scale is in the range (0-1)
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        CGAffineTransform transform = CGAffineTransformScale([[pinchGestureRecognizer view] transform], newScale, newScale);
    [pinchGestureRecognizer view].transform = transform;

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


}
   if ([pinchGestureRecognizer state] == UIGestureRecognizerStateEnded) {
          [undoList addObject:cropImage];
    }
}

我要回答我自己的问题。

我搜索了很多文章和几个小时的调试我发现当滚动视图被缩放时,实际上它的 contentSize 增加了它的 zoomScale 保持不变。

所以我只是使用了 scrollview 的子视图(即 self.containerView)的 transform 属性 并设置了 scrollview 的 contentOffset 我得到了我想要的东西。

self.containerView.transform = CGAffineTransformMakeScale(1.8, 1.8); // containerView is subview of scrollview and 1.8 is zoom scale just for eg.
[self.scrlVPhoto setContentOffset:CGPointMake(self.scrlVPhoto.contentOffset.x, (self.scrlVPhoto.contentOffset.y + cropRect.origin.y)) animated:true];