无尽的 iPad 滑动?
我想实现一个 ipad 视图,在其中解析 xml 文件并将条目并排放置在“无尽”的 ipad 视图上,因此您必须向左和向右滑动它。有人可以告诉我如何实现这个吗?我必须使用哪种类型的视图?
预先感谢
问候
i want to implement a ipad view, where i parse an xml file and put the entries side by side on an "endless" ipad view, so you have to swipe through it left and right. could someone tell me how i can implement this? which type of view do i have to use?
thanks in advance
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用非常大的
contentSize
并不是正确的方法。contentSize
仍然使用固定的数据类型,如果你滚动足够长的时间,它们就会溢出,最多你的绘图会变得混乱。最坏的情况是,您的应用程序崩溃。您想要的是通过使用窗口给人无限滚动的印象。我将通过一个简单的一维示例来说明它是如何工作的,但您可以轻松地将其扩展到二维。
假设您有 3 个条目,其中每个条目都填充
UIScrollView
。向右滚动,它看起来会像这样排列:在内部,您将像这样排列它们:
因为当 A 可见时,如果向右滑动,您可以看到 C 的一部分,如果向右滑动,您可以看到 B 的一部分向左滑动。
您的
UIScrollView
的contentOffset
是您的窗口。虽然contentSize
包含所有四个实体 (CAB C
) 的宽度,但在内部您将其限制为实际宽度的 75%。当用户左右滚动时,您可以调整contentOffset
,使其永远不会为负数或超过contentSize.width
的 75%。这是在您的委托内部完成的,在scrollViewDidScroll:
中,请注意,这假设有一个实例变量
constrainedContentSize
,可能在您的UIScrollView
视图的控制器中。位于内部,并且控制器是您的 UIScrollView 委托。这比不断释放和重新创建视图要高效得多。
Using a really big
contentSize
isn't the way to go.contentSize
still uses fixed datatypes, and if you scroll long enough, they'll overflow and at best, your drawing will go haywire. Worst case, your app crashes.What you want is to give the impression of infinite scrolling by using a window. I'll illustrate how it works with a simple 1-D example, but you can easily extend it to 2-D.
Let's say you have 3 entries, where each one fills the
UIScrollView
. Scrolling to the right, it would appear to be arranged like this:Internally, you're going to arrange them like this:
Because when A is visible, you can see part of C if you swipe to the right or part of B if you swipe to the left.
Your
UIScrollView
'scontentOffset
is your window. WhilecontentSize
encompasses the width of all four entities (C A B C
), internally you're going to restrict that to 75% of the actual width. As your user scrolls left and right, you adjustcontentOffset
so that it is never negative or more than 75% ofcontentSize.width
. This is done inside your delegate, inscrollViewDidScroll:
Note that this assumes an instance variable
constrainedContentSize
, likely in the controller for the view that yourUIScrollView
is inside, and that the controller is yourUIScrollView
delegate.This will be far more efficient than constantly releasing and recreating views.
您使用启用分页的 UIScrollView。要点是将 myscrollView.contentSize 设置为所有页面的总宽度,但在滚动发生时及时创建各个页面(请参阅 UIScrollViewDelegate 文档);换句话说,当您需要的只是三个视图(当前视图以及上一个视图和下一个视图)时,您实际上不希望有数十个或更多视图耗尽内存。苹果的这个示例代码应该可以帮助您入门:
http://developer.apple.com/iphone/library/示例代码/滚动/简介/Intro.html
You use UIScrollView with paging enabled. The essentials will be setting myscrollView.contentSize to the total width of all your pages, but creating the individual pages on a just-in-time basis as the scrolling is happening (see the UIScrollViewDelegate documentation); in other words you don't want to actually have dozens or more views using up memory when all you need is three -- the current view and plus the previous and next views. This sample code from apple should get you started:
http://developer.apple.com/iphone/library/samplecode/Scrolling/Introduction/Intro.html