UIScrollView 具有多个缩放级别?
有没有一种方法可以控制 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可能正在寻找的方法是
UIScrollViewDelegate
的- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
这可以让您返回您想要的视图要应用的缩放,在您的情况下,这将是背景地图图像。
如果您不希望同时缩放标记,则需要将标记直接添加为
UIScrollView
的子视图,而不是将它们添加为背景地图的子视图UIImageView。
正如您所提到的,标记是相对于地图背景
UIImageView
放置的,这意味着如果您将标记添加到UIScrollView
中,它们将不会放置在相同的位置更改滚动后在地图上的位置。为了解决这个问题,您需要计算相对于
UIScrollView
的等效坐标。虽然这看起来相当令人畏惧,但一旦您知道了缩放系数,实际上就很简单了。
要获取缩放系数,请使用
UIScrollViewDelegate
方法然后您可以通过调用 : 来获取缩放系数,
并通过将此
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 mapUIImageView
.As you mentioned, the markers are placed relatively to the map background
UIImageView
, this means if you add the markers to theUIScrollView
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
methodThen you can get the zoom factor by calling :
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 :)