Alex Stone
Alex Stone

Reputation: 47348

iPhone iOS calculate location within a UIScrollView with zooming

I have a color map within a UIScrollView and am trying to sample the color of a pixel of this map. The sample reticle is positioned above the scrollview, while the user moves the contents of the scrollview under the reticle.

The user can drop the reticle with a tap gesture, but I would like to offer an extra option of moving the view under the reticle.

I'm trying to find out how I can understand what x,y coordinate of the zoomed view is currently under the reticle. The logic for this so far eludes me, especially since zooming in/out is involved.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    CGPoint mapLocation = [tapGestureRecognizer locationInView:self.surfaceMap];

     NSLog(@"mapLocation (x,y) %.0f,%.0f",mapLocation.x,mapLocation.y);

    NSLog(@"contentOffset (x,y) %.0f,%.0f",self.scrollView.contentOffset.x,self.scrollView.contentOffset.y);

    //calculate where the marker is pointing to in the surface map while the scrollview is scrolling

    int frameWidth = self.surfaceMap.frame.size.width;
    int frameHeight = self.surfaceMap.frame.size.height;

//this is what I'm trying to calculate
    CGPoint trueLocation = CGPointMake(self.scrollView.contentOffset.x+frameWidth-self.surfaceMap.frame.origin.x, self.scrollView.contentOffset.y-self.surfaceMap.frame.origin.y);
    NSLog(@"trueLocation (x,y) %.0f,%.0f",trueLocation.x,trueLocation.y);

    [self colorOfPixelAtPoint:trueLocation];

}

Any input is appreciated!

Upvotes: 0

Views: 1823

Answers (1)

Jay Wardell
Jay Wardell

Reputation: 2290

You may want to a have look at these two methods in UIView:

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

Upvotes: 1

Related Questions