开发者

Get the zoomed frame of UIScrollView relative to original size

开发者 https://www.devze.com 2023-01-18 10:07 出处:网络
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?

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号