简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM