How can I determine the area currently visible in

2019-08-15 09:41发布

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.

2条回答
狗以群分
2楼-- · 2019-08-15 10:18

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 );
查看更多
做自己的国王
3楼-- · 2019-08-15 10:30

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];
查看更多
登录 后发表回答