RLH
RLH

Reputation: 15698

Scroll Views, Zooming and Zoom Origin Confusion

I've been reading through quite a bit of documentation, and I must say, I'm quite confused. Here is what I have currently have setup.

In an iPad project, I have a UIScrollView (scrollView) that contains a child UIView (insetView). My insetView is returned from the scrollViewDidEndZooming method and is used strictly for zooming it's child views.

insetView starts out smaller than and centered on scrollView. As the user pinches to Zoom, insetView can either grow to twice it's original size, or shrink to half of it's original size.

All of this works fine but there is one little annoyance that I want to alleviate. If I pinch to zoom, insetView shrinks, but the origin of the zooming is the top-left corner of insetView. In other words, insetView will both grow and shrink with it's top-left point fixed in the scrollView window.

What I would like to do is have insetView zoom in and out on it's center, and stay fixed at the center of scrollView until it is sized larger than the scrollView area. At that point, I would like for the zoom origin to be the middle of the pinch or, at least, at the center of the current portion of insetView that is currently visible?

How can this be accomplished?

Upvotes: 1

Views: 572

Answers (1)

mattjgalloway
mattjgalloway

Reputation: 34912

You want to do something like this in your scrollViewDidZoom: delegate method:

CGSize boundsSize = scrollView.bounds.size;
CGRect frameToCenter = insetView.frame;

if (frameToCenter.size.width < boundsSize.width) {
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2.0f;
} else {
    frameToCenter.origin.x = 0.0f;
}

if (frameToCenter.size.height < boundsSize.height) {
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2.0f;
} else {
    frameToCenter.origin.y = 0.0f;
}

insetView.frame = frameToCenter;

Upvotes: 2

Related Questions