UIScrollView 具有多个缩放级别?

发布于 2024-12-25 19:20:29 字数 297 浏览 9 评论 0原文

有没有一种方法可以控制 UIScrollView 的哪些子视图缩放,哪些不缩放?

例如,我有一个地图作为底层(不是 MapKit,只是一个平面图像),可以缩放和平移。根据用户的选择,标记被放置在地图上以指示特定的建筑物/地点等。这些标记也是 UIImageView,使用地图图像的像素坐标(例如建筑物 X 位于 (934, 842),放置标记这里)。然而,缩小后这些标记有点难以看到,因为它们也会缩放/缩小到与地图相同的水平。

那么有没有一种方法可以告诉 UIScrollView 不要缩放标记图像,但仍然允许它在地图图像缩放时平移/重新定位它们?

Is there a way that I can control which subviews of a UIScrollView are scaled and which are not?

For example, I have a map as the bottom layer (not MapKit, just a flat image) which can be zoomed and panned. Depending on user selections, markers are dropped on the map to indicate specific buildings/places etc. These markers are are also UIImageView, using pixel co-ordinates of the map image (eq Building X is at (934, 842), marker is placed here). Zoomed out however these markers are somewhat difficult to see as they also scale/zoom out to the same level as the map.

So is there a way that I can tell the UIScrollView NOT to scale the marker images, but still allow it to pan/reposition them when the map image is zoomed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2025-01-01 19:20:29

我认为您可能正在寻找的方法是 UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

这可以让您返回您想要的视图要应用的缩放,在您的情况下,这将是背景地图图像。

如果您不希望同时缩放标记,则需要将标记直接添加为 UIScrollView 的子视图,而不是将它们添加为背景地图的子视图 UIImageView。

正如您所提到的,标记是相对于地图背景 UIImageView 放置的,这意味着如果您将标记添加到 UIScrollView 中,它们将不会放置在相同的位置更改滚动后在地图上的位置。

为了解决这个问题,您需要计算相对于 UIScrollView 的等效坐标。

虽然这看起来相当令人畏惧,但一旦您知道了缩放系数,实际上就很简单了。

要获取缩放系数,请使用 UIScrollViewDelegate 方法

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

然后您可以通过调用 : 来获取缩放系数,

scrollView.zoomScale

并通过将此 zoomScale 应用于原始值来重新计算标记坐标,然后重新定位相应地你的标记。

参考:

UIScrollView类参考

UIScrollViewDelegate 协议参考

希望这有帮助:)

I think the method you may be looking for is the UIScrollViewDelegate's

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

This lets you return the view which you want the zoom to be applied to, in your case this will be the background map image.

If you don't want the markers to be scaled at the same time, you will need to add the markers directly as subviews of the UIScrollView, instead of adding them as subviews of your background map UIImageView.

As you mentioned, the markers are placed relatively to the map background UIImageView, this means if you add the markers to the UIScrollView instead, they won't be placed in the same position on the map once you change the scroll.

To solve this, you will need to calculate the equivalent coordinates relative to the UIScrollView.

While this may seem rather daunting, it is actually quite trivial to do once you know the zoom factor.

To get the zoom factor, use the UIScrollViewDelegate method

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

Then you can get the zoom factor by calling :

scrollView.zoomScale

and recalculate the marker coordinates by applying this zoomScale to the original values, and reposition your markers accordingly.

Reference:

UIScrollView Class Reference

UIScrollViewDelegate Protocol Reference

Hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文