在ZoomToRect之后的UIScrollview计算新的CGRect

时间:2013-03-22 09:25:31

标签: ios objective-c uiscrollview zoom

我的问题是,在CGRect使用UIScrollview zoomToRect方法进行缩放后,我无法获得准确的UIScrollview值。在缩放时我正在设置contentOffset属性。

代码如下:

if(image != nil) {

        CGSize zoomViewSize = image.frame.size;


        CGSize scrollViewSize = self.bounds.size;

        if(zoomViewSize.width < scrollViewSize.width) {
            anOffset.x = -(scrollViewSize.width - zoomViewSize.width) / 2.0;
        }

        if(zoomViewSize.height < scrollViewSize.height) {
            anOffset.y = -(scrollViewSize.height - zoomViewSize.height) / 2.0;
        }
    }
super.contentOffset = anOffset;

在缩放后我设置了内容插入属性,以便图像位于中心。对于内容插入,代码如下所示。

-(void)zoomtorect:(CGRect)rect animated:(BOOL)animated {

[super zoomtorect:rect animated:YES];


CGFloat pageWidth = image.image.size.height;



         CGSize imageSize = rect.size;
         CGSize zoomedImageSize = CGSizeMake(imageSize.width * 0.2, imageSize.height * 0.2);
         CGSize pageSize = self.bounds.size;

         //scLayer.frame = rect;
         UIEdgeInsets inset = UIEdgeInsetsZero;
         if (pageSize.width > zoomedImageSize.width) {
             inset.left = (pageSize.width - zoomedImageSize.width) / 2;
             inset.right = inset.left;
         }
         if (pageSize.height > zoomedImageSize.height) {
             inset.top = (pageSize.height - zoomedImageSize.height) / 2;
             inset.bottom = inset.top;
         }

         self.contentInset = inset;
}

此代码在屏幕之间移动我的图像视图。这里的每件事都很好。我的问题是我必须只显示imageView休息的缩放部分将是黑色的。为了计算缩放区域,我正在进行以下计算。

CGSize imageSize = rect.size;
    CGSize zoomedImageSize = CGSizeMake(imageSize.width * scroll.maximumZoomScale, imageSize.height *scroll.maximumZoomScale);
    CGSize pageSize = scroll.bounds.size;

    UIEdgeInsets inset = UIEdgeInsetsZero;
    if (pageSize.width > zoomedImageSize.width) {
        inset.left = (pageSize.width - zoomedImageSize.width) / 2;
        inset.right = inset.left;
    }
    if (pageSize.height > zoomedImageSize.height) {
        inset.top = (pageSize.height - zoomedImageSize.height) / 2;
        inset.bottom = inset.top;
    }

    CGRect zoomRect = [scroll convertRect:rect fromView:image];

    CGRect zoomRect1 = CGRectMake(inset.left, inset.top+50, zoomRect.size.width, zoomRect.size.height);

这个zoomRect1是图像的缩放区域,这个值与我在屏幕上看到的不完全相同。一些我在做计算错误的地方请帮帮我。但我没有得到缩放图像区域的确切值。

提前致谢

1 个答案:

答案 0 :(得分:1)

使用此.. 完美运作

- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer
{
    if(isAlreadyZoomed)
    {
        CGPoint Pointview = [recognizer locationInView:recognizer.view];
        CGFloat newZoomscal = 3.0;
        CGSize scrollViewSize = self.scrollContainer.bounds.size;
        CGFloat width = scrollViewSize.width/newZoomscal;
        CGFloat height = scrollViewSize.height /newZoomscal;
        CGFloat xPos = Pointview.x-(width/2.0);
        CGFloat yPos = Pointview.y-(height/2.0);
        CGRect rectTozoom=CGRectMake(xPos, yPos, width, height);
        [self.scrollContainer zoomToRect:rectTozoom animated:YES];
        [self.scrollContainer setZoomScale:3.0 animated:YES];
        isAlreadyZoomed = NO;
    }
    else
    {
        [self.scrollContainer setZoomScale:1.0 animated:YES];
        isAlreadyZoomed = YES;
    }
}

isAlreadyZoomedBOOL