UIScrollView 缩放时滚动到矩形?
我有一个 UIImageView,其中有一个 UIScrollView 内的图像(地图)。我将另一个图像(标记)添加到图像视图(因此地图背景和标记)。如果屏幕上未显示标记,则当我放置标记时我想滚动以显示标记。
将标记放置在地图上后,我正在使用 [scrollViewscrollRectToVisible:rectanimated:YES]
。当滚动视图未缩放时,这非常有用。如何根据缩放比例调整 rect
以使标记在视图中居中?
I have an UIImageView with an image (map) inside of a UIScrollView. I add another image (marker) to the image view (so a map background and a marker). If the marker isn't shown on the screen, when I place the marker I want to scroll to show the marker.
I'm using [scrollView scrollRectToVisible:rect animated:YES]
after I place the marker on the map. This works great when the scrollview isn't zoomed. How do I adjust rect
based on the zoom scale so the marker is centered in the view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在当前正在开发的应用程序中做了一些非常类似的事情,但我没有使用
-scrollRectToVisible
,而是使用-setContentOffset
。因为我知道标记在框架中的位置,所以我可以轻松计算其中心,并将其保存为 CGFloats
centerX
和centerY
。然后,我使用以下代码将标记在屏幕上居中(我从 Y 值中减去 161,因为我的主视图有一个选项卡栏和一个导航控制器,所以它不是完整的 480 个点):当前的 ZoomScale,无论缩放级别如何,我都可以找到正确的中心点。
我的标记是保存我的地图的 UIImageView 的子视图。听起来你正在以同样的方式做这件事,所以我想这个(或类似的东西)应该适合你。
I do something very similar in the app I am currently working on, but instead of using
-scrollRectToVisible
, I use-setContentOffset
.Because I know where my marker is in the frame, I can easily calculate its center, which I save as the CGFloats
centerX
andcenterY
. I then use the following code to center the marker on the screen (I subtract 161 from the Y value because my main view has a tab bar and a navigation controller, so it is not the full 480 points):By multiplying the center point by the current zoomScale, I can find the correct center point regardless of zoom level.
My marker is a subview of the UIImageView that holds my map. It sounds like you are doing it the same way, so I imagine this (or something similar) should work for you.