views:

57

answers:

1

Hi All,

Could somebody advice which formula used to content offset calculation after zoom in UIScrollView? Let's consider following example: I have a UIScrollView with content view in size (1000, 1000), then if I programmatically setZoomScale to 2.0 and in 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} calculated. Is there any dependency of formula that used to calculate the content offset in this case?

Thanks

A: 

This may or may not be relevant, but note that 160x230 is half the resolution of the iPhone, less the status bar: 320x460. Try changing the UIScrollView's frame, or its superview's frame, and see how this affects the numbers.

EDIT: Come to think of it, it makes perfect sense that the offset is half the size of the scroll view, as it will expand equally 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 was 4.0f, the offset would be: (320/2 * (4-1), 460/2 * (4-1)) => (480, 690). Try a scale of 4 and see if (480, 690) comes out.

David Foster
Thanks a lot, David! That is exact what I need.