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:
When the
zoomingView
is smaller thanbounds
,contentOffset
reflect not the top-left corner of content view, but some strange shift of content view relative to the center ofbounds
. AndzoomingView.frame.origin
has normal values as ifzoomingView
were in the center ofbounds
. (this happens if you try to shrink the zoomingView more thanminimulScale
)When the
zoomingView
is bigget thanbounds
,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.
精彩评论