开发者

Scroll programmatically an UITableView with acceleration and deceleration

开发者 https://www.devze.com 2023-04-12 04:54 出处:网络
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 i开发者_JS百科n scrolling time.

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 i开发者_JS百科n 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!


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


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];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号