Search code examples
iphoneobjective-ciosuiscrollview

How to zoom a view in UIScrollView with anchor point in center?


I have a view in UIScrollView in which the user can zoom into.

The view has the same size as the UIScrollView frame. However, the subviews of that view are bigger and centered. It's a container with same size as UIScrollView with centered content.

When zooming out, UIScrollView changes the scale of the content with an awkward anchor point to the upper left, rather than a centered one.

Is there a way to change this behavior such that when zooming in or out the zoom happens relative to the center rather than the upper left corner?


Solution

  • Yes I believe you can change the anchorPoint for a view's layer. like so -

    [self.yourView.layer setAnchorPoint:CGPointMake(0.5, 0)];
    

    The coordinates of the anchor point are aptly defined in this diagram -

    enter image description here

    If you want the animation to anchor from left-center then set the anchorPoint to (0.0, 0.5) i.e. point B in the figure.