Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll programmatically an UITableView with acceleration and deceleration

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!

like image 253
Stan Bright Avatar asked Oct 12 '11 06:10

Stan Bright


1 Answers

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
like image 173
Albert Tong Avatar answered Nov 05 '22 17:11

Albert Tong