Manny
Manny

Reputation: 6287

Get the zoomed frame of UIScrollView relative to original size

I have a UIScrollView and added a UIView inside it, when I zoom it, is it possible to get the CGRect of the zoomed frame in relation to the original frame?

E.g. i have 800x600 frame, then i zoomed to {{50, 60}, {100, 100}} is it possible to programmatically get the zoomed frame?

Upvotes: 9

Views: 2723

Answers (2)

DanSkeel
DanSkeel

Reputation: 3970

The problem with @Vladimir solution is that it displays visibleRect wrong if viewForZoom is smaller than ScrollView bounds. So I came up with this solution.

- (CGRect) zoomedFrame{
    CGRect zoomedFrame;

    zoomedFrame.origin = self.contentOffset;
    zoomedFrame.origin.x -= zoomingView.frame.origin.x;
    zoomedFrame.origin.y -= zoomingView.frame.origin.y;
    zoomedFrame.size = self.contentSize;
    return zoomedFrame;
}

zoomingView is a view that returns viewForZoomingInScrollView: method.
bounds are bounds of scrollView.


So there are two cases:

  1. When the zoomingView is smaller than bounds, contentOffset reflect not the top-left corner of content view, but some strange shift of content view relative to the center of bounds. And zoomingView.frame.origin has normal values as if zoomingView were in the center of bounds. (this happens if you try to shrink the zoomingView more than minimulScale)

  2. When the zoomingView is bigget than bounds, zoomingView.frame.origin has strange values like this:

    {-6.15367e-06, 3.98168e-06}

    And contentOffset shows what it should.


So all that compensate each other as I showed in my code.

Upvotes: 5

Vladimir
Vladimir

Reputation: 170829

I usually use the following method (added to the custom UIScrollView category):

- (CGRect) visibleRect{
    CGRect visibleRect;

    visibleRect.origin = self.contentOffset;
    visibleRect.size = self.bounds.size;

    visibleRect.origin.x /= self.zoomScale;
    visibleRect.origin.y /= self.zoomScale;
    visibleRect.size.width /= self.zoomScale;
    visibleRect.size.height /= self.zoomScale;
    return visibleRect;
}

Upvotes: 10

Related Questions