Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView: content offset calculation after zoom

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

like image 647
user478681 Avatar asked Oct 17 '10 19:10

user478681


People also ask

What is the default value of contentsize in UIScrollView?

The default value is 0 for top, bottom, right, and left. contentSize is the size of the content within the UIScrollView and how long it can be within scrollView. Sometimes this is dynamic like pagination or static like a contact list. This might be changing at runtime as well. This can be set programmatically as well.

How does the scroll view change the scale of the content?

As the user makes a pinch-in or pinch-out gesture, the scroll view adjusts the offset and the scale of the content. When the gesture ends, the object managing the content view should update subviews of the content as necessary.

What is UIScrollView in UIKit?

UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView. A scroll view is a view with an origin that’s adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application’s main window.

What is Scroll View in UITableView?

A view that allows the scrolling and zooming of its contained views. UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView. A scroll view is a view with an origin that’s adjustable over the content view.


1 Answers

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.

like image 87
David Foster Avatar answered Oct 17 '22 01:10

David Foster