以编程方式滚动带有加速和减速的 UITableView
我正在尝试通过点击按钮以编程方式滚动 UITableView。用手做应该有同样的效果,甚至滚动时间更长一点。
首先,我尝试使用 scrollToRowAtIndexPath:atScrollPosition:animated: 但它太快并且及时修复。而且它没有任何加速和减速。
有什么想法如何以编程方式滚动 UITableView,因为它是手动滚动的?我正在考虑向它发送一些自定义触摸。你认为这可行吗?
非常感谢任何想法和帮助!谢谢!
I'm trying to scroll a UITableView programmatically by tapping a button. It should be the same effect at doing it by hand or even a little bit longer in scrolling time.
First I tried to use scrollToRowAtIndexPath:atScrollPosition:animated: however it too fast and fixed in time. Also it does not have any acceleration and deceleration.
Any ideas how to scroll a UITableView programmatically as it has been scrolled by hand? I was thinking to send some custom touches to it. Do you think it could work?
Any ideas and help are greatly appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UITableview 是 UIScrollview 的子类。与 UIScrollview 一样,您可以通过设置 contentOffset 属性以编程方式设置 UITableview 的滚动位置。
您可以在动画块内更改此属性值,以便您可以更好地控制时间。您需要计算 Y 点位置。假设您想滚动到第 50 行,它可能是 50 * rowHeight(假设它是统一高度)。
例如:
您可以使用另一种方法来提供更多选项,
看看这个方法:
UITableview is a subclass of UIScrollview. Like a UIScrollview, you can programmatically set UITableview's scroll position by setting the contentOffset property.
You change this property value inside an animation block so you have more control in the timing. You'll need to calculate the Y point position. Say you want to scroll to the 50th row, it might be 50 * rowHeight (assuming it's uniform height).
For example:
There's another method you could use that provides more options as well
Take a look at this method:
在单元格子视图中设置按钮,并使用此代码在按钮的触摸事件上以编程方式滚动表格。如果您没有将按钮设置为表格单元格的子视图,则根据需要修改此代码。
Set your button in cell subview and use this code to scroll table programmatically on touch event of button. And if you are not setting button as a subview of table cell then modify this code as needed.
好吧,这些只是一些“我突然想到的答案”,但您可以尝试使用此方法(在
UIScrollView
中,其中UITableView
是一个子类) :一旦
-(CGRect) rectForRowAtIndexPath:
为您提供单元格的坐标,您就可以动画到单元格的位置。但不要调用它一次,而是使用较小的偏移量调用它几次,然后使用越来越大的偏移量,然后再次使用较小的偏移量。不过,我担心很难让它看起来光滑。不过,如果这不起作用,也许更改
UIView animateWithDuration:delay:options:animations:completion
块内的contentOffset
值会起作用。也许如果你使用UIViewAnimationOptionCurveEaseInOut,你只需调用一次就可以获得你想要的效果。如果没有,请在每个部分运行几个动画,并在放慢速度之前覆盖越来越远的距离。Well, these are just a couple of "off the top of my head answers," but you could try using this method (in
UIScrollView
, of whichUITableView
is a subclass):Once
-(CGRect) rectForRowAtIndexPath:
gives you the coordinates of the cell, you can then animate to the location of cell. But rather than calling it once, call it a couple of times with a smaller offset, then with a larger and larger offsets and then a smaller ones again. I am worried that it will be difficult to make it look smooth, though.Still, if that doesn't work, perhaps changing
contentOffset
value inside of aUIView animateWithDuration:delay:options:animations:completion
block would work. Maybe if you use the UIViewAnimationOptionCurveEaseInOut, you can get the effect you want just calling it once. If not, run several animations each part of way and covering great and greater distances before slowing down.