水平循环UIScrollView [英] Looping UIScrollView Horizontally

查看:64
本文介绍了水平循环UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望循环一个UIScrollView,但不是以大多数方式循环浏览一个视图列表,而只是循环滚动一个子视图,然后无缝循环.

I wish to loop a UIScrollView, but not in the manner of most that loop through a list of views, just one that scrolls through one subview, but loops around seamlessly.

本质上,我正在尝试制作地图视图,以便可以放大和缩小并保持不断滚动.

Essentially, I'm trying to make a map view, that I can zoom in and out of and keeps scrolling around and around.

有人知道什么提示吗?

推荐答案

前段时间,我正在玩类似的问题,并且一起编写了一个解决方案的快速演示.临时而言,我不确定由很多图块构成的地图视图是否更容易或更难管理.

I was playing around with the same sort of problem a while ago and hacked together a quick demo of one solution. Off hand I'm not sure if a map view, which might be built of many tiles, would be easier or harder to manage.

https://github.com/jonah-carbonfive/WrappingScrollView

这是我在做的事情,以防代码中不清楚.

Here's what I'm doing in case it isn't clear from the code.

  1. 创建一个包含要显示的内容的单一视图.我们将其称为平铺视图".
  2. 以UIScrollView的内容视图为中心显示您的图块视图.使内容视图比图块视图大得多(请参阅下文以了解大图).
  3. 为图块视图的内容创建图像.
  4. 创建包含该图像的UIImageView"tiles",并将其放置在平铺视图周围,以填充滚动视图的内容视图.可能会有很多UIImageView,但是它们共享一个图像,因此内存占用空间还不错.
  5. 现在您有一个大的滚动视图,看起来至少可以包装一次它的内容,但是如果滚动得足够远,您仍然会碰到边缘.
  6. 在滚动视图的-scrollViewDidScroll:委托方法中,检查平铺视图是否完全滚动到视线之外,如果这样,请重新定位滚动视图的内容偏移量以重新定位内容视图,以便平铺视图回到可见区域的中间.做得正确的用户永远不会注意到您从查看平铺视图的图像跳回到了平铺视图本身.
  7. 但是等等!实际上只有中央图块视图会响应触摸.毕竟,它的所有其他副本只是图像.我们可以通过重写-hitTest:withEvent:来重新定位图块视图,以代替用户在调用super之前尝试触摸的任何图像视图,来解决此问题.现在,所有触摸都可以到达图块视图.
  8. 确保每当重新绘制图块视图时都更新图块视图的图像.
  1. Create a single view containing whatever content you want to display. We'll call this the "tile view".
  2. Display your tile view centered in a UIScrollView's content view. Make the content view significantly larger than the tile view (see below for how much larger).
  3. Create an image of the contents of your tile view.
  4. Create UIImageView "tiles" containing this image and position them around the tile view to fill your scroll view's content view. That might be a lot of UIImageViews but they share a single image so the memory footprint isn't too bad.
  5. Now you have a large scroll view which appears to wrap it's contents at least once but you'll still hit the edge if you scroll far enough.
  6. In the scroll view's -scrollViewDidScroll: delegate method check if your tile view has scrolled completely out of sight, if so reposition the scroll view's content offset to reposition the content view so the tile view is back in the middle of the visible region. Done correctly the user never notices that you jumped from looking at an image of the tile view back to the tile view itself.
  7. But wait! Only the central tile view will actually respond to touches. All the other copies of it are just images after all. We can work around this by overriding -hitTest:withEvent: to reposition the tile view to take the place of whatever image view the user attempts to touch before calling super. Now all touches reach the tile view.
  8. Make sure to update the image of the tile view any time the tile view is redrawn.

这有点疯狂,但是它让我将任意交互式UIViews和UIControls扔进了一个容器中,并让它们从一侧包裹到另一侧.

That's kind of crazy but it let me throw arbitrary interactive UIViews and UIControls into a container and let them wrap off one side and back onto the other.

老实说,这对您来说可能不是一个很好的解决方案.如果我知道我想画一张可以包裹一个或多个尺寸的地图,我会将地图分成许多图块.就像UIScrollView一样,检测某些容器视图上的触摸,并在用户移动触摸时重新定位图块.移除看不见的磁贴并重新使用它们以显示地图的新可见区域. 在简单的情况下,您可能只需要使用两张地图图像即可实现此目的.在一个复杂的情况下,您将重新创建许多UIScrollView.缩放显示的图块,并可能以不同的分辨率加载不同的图块集,以减少内存使用并在缩放时提高分辨率.

Honestly this is probably not a great solution for you. If I knew that I wanted to draw a map which could wrap in one or more dimensions I would break the map into many tiles. Detect touches on some container view much like UIScrollView does, and reposition the tiles as the user moves their touches. Removing tiles as they scroll out of sight and reusing them to show newly visible regions of the map. In a simple case you might be able to implement this using just two copies of a map image. In a complex case you're recreating much of UIScrollView; scaling the tiles you display and possibly loading different tile sets at different resolutions to reduce memory use and increase resolution while zooming.

这篇关于水平循环UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆