UIScrollView: Calculating Content Offsets After Scaling

Can anyone advise which formula is used to calculate the offset content after zooming in on the UIScrollView? Consider the following example: I have a UIScrollView with a content representation of (1000, 1000), and then if I programmatically setZoomScale up to 2.0 and in the scrollViewDidEndZooming:withView:atScale method, I will have the following:

 contentSize before zoom = {1000, 1000} contentOffset before zoom = {0, 0} scale = 2.000000 contentSize after zoom = {2000, 2000} contentOffset after zoom = {160, 230} 

I need to know how the new value of contentOffset {160, 230} is calculated. Is there a dependency on the formula used to calculate the content bias in this case?

thanks

+4
source share
1 answer

This may or may not be relevant, but note that 160x230 is half the resolution of the iPhone, with the exception of the status bar: 320x460. Try changing the UIScrollView frame or its supervisor frame and see how it affects numbers.

EDIT . Think about it, it makes sense that the offset is half the size of the scroll, as it will expand evenly in both directions. So the formula would be: contentOffset = (scrollView.frame.size.width/2 * (scaleAfter - scaleBefore), scrollView.frame.size.height/2 * (scaleAfter - scaleBefore)) .

Therefore, if the scale is 4.0f, the offset will be: (320/2 * (4-1), 460/2 * (4-1)) => (480, 690) . Try scale 4 and see if it comes out (480, 690).

+8
source

All Articles