繁体   English   中英

如何确定滚动视图中当前可见的区域并确定中心?

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

我有一个地图应用程序,用户可以手动放置航点。 我希望他们按下航点按钮,并在内容视图的当前可见视图的中心放置一个航点。

我担心你必须自己计算。 contentSize返回滚动内容的大小, contentOffset为您提供内容中滚动视图的来源。 然后使用scrollView.bounds.size您可以找到视图的中心。

没有测试过这个,但也许您可以将scrollView.center转换为滚动的地图,如下所示:

CGPoint viewportCenterInMapCoords = 
        [scrollView.superview convertPoint:scrollView.center
                                    toView:mapViewInsideScrollView];

需要考虑它是如何缩放的,然后我可以将内容偏移转换为完整图像的大小并添加一些。 ///这是地图图像的全尺寸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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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