How can I determine the area currently visible in a scrollview and determine the center?

与世无争的帅哥 提交于 2019-12-11 14:39:08

问题


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.


回答1:


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];



回答2:


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 );


来源:https://stackoverflow.com/questions/9503836/how-can-i-determine-the-area-currently-visible-in-a-scrollview-and-determine-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!