Reputation: 3506
I have a map app where the user can place waypoints manually. I would like for them to press the waypoint button and have a waypoint placed in the center of their currently visible view on the content view.
Upvotes: 1
Views: 767
Reputation: 3506
Need to account for how zoomed it is, then I can convert the content offset to the size of the full image and add some. /// this is the full size of the map image CGSize fullSize = CGPointMake(13900, 8400);
/// determines how the current content size compares to the full size
float zoomFactor = size.width/self.contentSize.width;
/// apply the zoom factor to the content offset , this basically upscales
/// the content offset to apply to the dimensions of the full size map
float newContentOffsetX = self.contentOffset.x*zoomFactor + (self.bounds.size.width/2) *zoomFactor-300;
float newContentOffsetY = self.contentOffset.y*zoomFactor + (self.bounds.size.height/2) * zoomFactor-300;
/// not sure why i needed to subtract the 300, but the formula wasn't putting
/// the point in the exact center, subtracting 300 put it there in all situations though
CGPoint point = CGPointMake(newContentOffsetX,newContentOffsetY );
Upvotes: 2
Reputation: 3835
I'm afraid you'd have to calculate it yourself. contentSize
returns size of the scrolled content, contentOffset
gives you the origin of the scroll view inside the content. Then with scrollView.bounds.size
you can find the center of the view.
Haven't tested this, but maybe you could convert scrollView.center
to your scrolled map like this:
CGPoint viewportCenterInMapCoords =
[scrollView.superview convertPoint:scrollView.center
toView:mapViewInsideScrollView];
Upvotes: 2