以编程方式滚动带有加速和减速的 UITableView

发布于 2024-12-09 15:38:49 字数 282 浏览 1 评论 0原文

我正在尝试通过点击按钮以编程方式滚动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷炫老祖宗 2024-12-16 15:38:49

UITableview 是 UIScrollview 的子类。与 UIScrollview 一样,您可以通过设置 contentOffset 属性以编程方式设置 UITableview 的滚动位置。

您可以在动画块内更改此属性值,以便您可以更好地控制时间。您需要计算 Y 点位置。假设您想滚动到第 50 行,它可能是 50 * rowHeight(假设它是统一高度)。

例如:

CGFloat calculatedPosY = 50 * rowHeight // do you own calculation of where you'd like it.
[UIView animateWithDuration:0.2
     animations:^{theTableview.contentOffset = CGPointMake(0.0, calculatedPosY);}
     completion:^(BOOL finished){ }];

您可以使用另一种方法来提供更多选项,

看看这个方法:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

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:

CGFloat calculatedPosY = 50 * rowHeight // do you own calculation of where you'd like it.
[UIView animateWithDuration:0.2
     animations:^{theTableview.contentOffset = CGPointMake(0.0, calculatedPosY);}
     completion:^(BOOL finished){ }];

There's another method you could use that provides more options as well

Take a look at this method:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
千鲤 2024-12-16 15:38:49

在单元格子视图中设置按钮,并使用此代码在按钮的触摸事件上以编程方式滚动表格。如果您没有将按钮设置为表格单元格的子视图,则根据需要修改此代码。

  CGPoint point = [button.superview convertPoint:button.frame.origin toView:self.tableView];
  CGPoint contentOffset = self.tableView.contentOffset;
  contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
  [self.tableView setContentOffset:contentOffset animated:YES];

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.

  CGPoint point = [button.superview convertPoint:button.frame.origin toView:self.tableView];
  CGPoint contentOffset = self.tableView.contentOffset;
  contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
  [self.tableView setContentOffset:contentOffset animated:YES];
飘然心甜 2024-12-16 15:38:49

好吧,这些只是一些“我突然想到的答案”,但您可以尝试使用此方法(在 UIScrollView 中,其中 UITableView 是一个子类) :

-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

一旦 -(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 which UITableView is a subclass):

-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

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 a UIView 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文