如何“坚持”滚动时 UIScrollView 子视图到顶部/底部?
你可以在 Gilt 等 iPhone 应用程序中看到这一点。用户滚动视图,当滚动视图的其余部分在下面滑动时,子视图显然“粘”到一个边缘。也就是说,scrollView 中有一个文本框(或其他内容),当scrollView 到达视图顶部时,它会“粘”在那里,而视图的其余部分继续滑动。
所以,有几个问题。首先,可以通过“scrollViewDidScroll:”(在正常滚动期间)确定感兴趣的视图何时经过(或重新出现)。这里有相当多的粒度 - 委托调用之间的差异可能是一百个点或更多。也就是说,当您看到视图接近滚动视图的顶部时,您将打开静态显示在滚动视图顶部下方的视图的第二个副本。我没有对此进行编码,但看起来它缺乏真正的“粘”外观 - 视图将首先消失然后重新出现。
其次,如果执行 setContentOffset:animated,则不会收到委托消息(Gilt 不会执行此操作)。那么,在这种情况下如何获得回调呢?你在“scroll.layer.presentationLayer.bounds”上使用KVO吗?
You see this in iPhone apps like Gilt. The user scrolls a view, and a subview apparently "sticks" to one edges as the rest of the scrollView slides underneath. That is, there is a text box (or whatever) in the scrollView, that as the scrollView hits the top of the view, then "sticks" there as the rest of the view continues to slide.
So, there are several issues. First, one can determine via "scrollViewDidScroll:" (during normal scrolling) when the view of interest is passing (or re-appearing). There is a fair amount of granularity here - the differences between delegate calls can be a hundred of points or more. That said, when you see the view approach the top of the scrollView, you turn on a second copy of the view statically displayed under the scrollView top. I have not coded this, but it seems like it will lack a real "stick" look - the view will first disappear then reappear.
Second, if one does a setContentOffset:animated, one does not get the delegate messages (Gilt does not do this). So, how do you get the callbacks in this case? Do you use KVO on "scroll.layer.presentationLayer.bounds" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了一种方法来做到这一点。当用户通过轻拂和拖动进行滚动时,UIScrollView 会向其委托发送“scrollViewDidScroll:”消息。然后您可以查看滚动条是否已将内容移动到您需要执行某些操作的位置。
当“粘住”视图时,将其从滚动视图中删除,然后将其添加到滚动视图的超级视图中(原点为0,0)。脱粘时,进行相反的操作。
如果您使用 UIScrollView setContentOffset:animated:,它会变得更加棘手。我所做的是子类化 UIScrollView,使用一个标志来指定它是 setContentOffset 移动偏移量,然后启动一个快速运行的计时器来监视 contentOffset。
我将处理数学和粘贴/取消粘贴子视图的方法放入这个子类中。看起来不错。
Well, I found one way to do this. When the user scrolls by flicking and dragging, the UIScrollView gives its delegate a "scrollViewDidScroll:" message. You can look then to see if the scroller has moved the content to where you need to take some action.
When "sticking" the view, remove it from the scrollView, and then add it to the scrollView's superview (with an origin of 0,0). When unsticking, do the converse.
If you use the UIScrollView setContentOffset:animated:, it gets trickier. What I did was to subclass UIScrollView, use a flag to specify it was setContentOffset moving the offset, then start a fast running timer to monitor contentOffset.
I put the method that handles the math and sticking/unsticking the child view into this subclass. It looks pretty good.
Gilt 使用表视图来完成此任务。具体来说,在表视图的委托中,这两个方法:
–表视图:viewForHeaderInSection:
和 -tableView:heightForHeaderInSection:
Gilt uses a table view to accomplish this. Specifically, in the table view's delegate, these two methods:
– tableView:viewForHeaderInSection:
and – tableView:heightForHeaderInSection: