dimanche 28 juin 2015

Can't center UIScrollView around CGPoint on y axis, or when zoomed

I am trying to center a CGPoint inside the Visible Rectangle that is my UIScrollView.

Here is the setup:

enter image description here

I have a SpriteKit scene with some objects on it. This scene is basically the View for my UIScrollView. The Scene is much larger than the visible scene on the device, as show with the visible rectangle.

I want to set the contentOffset of the UIScrollView so that a point is in the center of the visible rect.

enter image description here

Here is what I have tried doing, to no avail:

    CGPoint adjustment = CGPointMake(visibleRect.size.width/2, visibleRect.size.height/2);
    CGPoint wantedMidPoint = node.position;

    CGPoint newOffset = CGPointMake(wantedMidPoint.x - adjustment.x, wantedMidPoint.y + adjustment.y);

The Offset is then set as follows:

[self.scrollView setContentOffset:newOffset animated:YES];

The problem is this does not work. What specifically? The Y Position.

In each case, the selected node does get centered. However, the Y coordinate is totally screwy. If I add the adjustment, it goes up or down inversely to what I want it to. If I subtract it, it totally flies off screen. I don't know how to fix this.

Also, if I zoom a little, the X position gets flung off track too. I don't understand why. I've tried scaling the offset adjustment up or down based on the scaling, but it doesn't help.

This wasn't my only attempt. I've tried using the visible rect of the UIScrollView this way too:

[scrollView convertRect:scrollView.bounds
                             toView:[scrollView.delegate viewForZoomingInScrollView:scrollView]];

If someone could explain how to do this, it would be very much appreciated.

Aucun commentaire:

Enregistrer un commentaire