Search code examples
objective-ciosxcodeios5

ConvertRect accounting for UIScrollView zoom and contentOffset


I've been trying to get the converted CGRect of a UIView within a UIScrollView. It works fine if I'm not zoomed, but once I zoom, the new CGRect shifts. Here is the code that's gotten me close:

CGFloat zoomScale = (scrollView.zoomScale);    
CGRect newRect = [self.view convertRect:widgetView.frame fromView:scrollView];
CGPoint newPoint = [self.view convertPoint:widgetView.center fromView:scrollView];

//  Increase the size of the CGRect by multiplying by the zoomScale
CGSize newSize = CGSizeMake(newRect.size.width * zoomScale, newRect.size.height * zoomScale);
//  Subtract the offset of the UIScrollView for proper positioning
CGPoint newCenter = CGPointMake(newPoint.x - scrollView.contentOffset.x, newPoint.y - scrollView.contentOffset.y);

//  Create rect with the proper width/height (x and y set by center)
newRect = CGRectMake(0, 0, newSize.width, newSize.height);    

[self.view addSubview:widgetView];

widgetView.frame = newRect;
widgetView.center = newCenter;

I'm fairly certain that my issue lies in the zoomScale - I should probably be modifying the x and y coordinates based on the zoomScale value. Everything I have tried thus far has been unsuccessful, though.


Solution

  • I received the following answer from user Brian2012 on the iOS dev forums:

    What I did:

    1. Created a UIScrollView that covers the view controller's main view.
    2. Put a desktop view (a standard UIView) in the scroll view. The origin of the desktop is at 0,0 and the size is bigger than the scroll view so I could scroll around without having to zoom first.
    3. Put some widget views (UIImageView) into the desktop view at various locations.
    4. Set the contentSize of the scroll view to the size of the desktop view.
    5. Implemented viewForZoomingInScrollView to return the desktop view as the view to scroll.
    6. Put NSLogs in scrollViewDidZoom to print out the frame of the desktop view and one of the widget views.

    What I found out:

    1. The widget frame never changes from the initial value that I set. So for example, if a widget started at position 108, 108 with a size of 64x64, then the frame is always reported as 108,108,64,64 regardless of zooming or scrolling.
    2. The desktop frame origin never changes. I put the origin of the desktop at 0,0 in the scroll view, and the origin is always reported as 0,0 regardless of zooming or scrolling.
    3. The only thing that changes is the desktop view's frame size, and the size is just the original size multiplied by the scroll view's zoomScale.

    Conclusion:

    To figure out the location of a widget relative to the coordinate system of the view controller's main view, you need to do the math yourself. The convertRect method doesn't do anything useful in this case. Here's some code to try

    - (CGRect)computePositionForWidget:(UIView *)widgetView fromView:(UIScrollView *)scrollView
    {
        CGRect frame;
        float  scale;
    
        scale = scrollView.zoomScale;
    
        // compute the widget size based on the zoom scale
        frame.size.width  = widgetView.frame.size.width  * scale;
        frame.size.height = widgetView.frame.size.height * scale;
    
        // compute the widget position based on the zoom scale and contentOffset
        frame.origin.x = widgetView.frame.origin.x * scale - scrollView.contentOffset.x + scrollView.frame.origin.x;
        frame.origin.y = widgetView.frame.origin.y * scale - scrollView.contentOffset.y + scrollView.frame.origin.y;
    
        // return the widget coordinates in the coordinate system of the view that contains the scroll view
        return( frame );
    }