【问题标题】:Zoom a specific area of a Scrollview缩放 Scrollview 的特定区域
【发布时间】:2016-09-01 12:10:42
【问题描述】:

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

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 是我要缩放的视图。

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

请有人可以帮助我。任何帮助表示赞赏。

提前致谢。

【问题讨论】:

标签: ios objective-c uiscrollview zooming


【解决方案1】:

我将回答我自己的问题。

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

所以我只是使用了滚动视图子视图的transform 属性(即self.containerView)并设置了滚动视图的 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];

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • self.containerView是scrollview的子视图,是scrollview的委托方法返回的。
    【解决方案3】:

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

    viewDidLoad 方法:

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

    DoubleTab 方法:

    - (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 及其对我的项目的工作,

    希望对你有帮助

    【讨论】:

    • 正如我所说,我想以编程方式缩放滚动视图...而不是通过触摸、手势等。
    【解决方案4】:

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

     -(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];
        }
    }
    

    【讨论】:

    • 我不想使用手势。我想以编程方式进行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多