Avi Avi - 10 months ago 285
iOS Question

UITableView :scrollToRowAtIndexPath or setContentOffset animation speed

Lets say i have 10 rows displaying numbers 1 to 10. I need scrolling effect like 1 to 10 to 1to 10... I achieved this using

. The issue i want to slow down the scrolling before i apply my logic to stop this recursion. So is there a way that i can decrease the scroll speed for
? Please do not suggest, decelerationRate as scrolls here are all programmatic, no user interactions. Thanks in advance.


You can do something like this:

[UIView animateWithDuration:durationInSeconds animations:^(void){
    [tableView setContentOffset:offset animated:NO];

You just need to calculate the offset. If your rows have equal heights, then your offset is simply calculated:

CGPoint offset = CGPointMake(0, indexPath.row * rowHeight);

and if the height varies then you need to do this:

CGFloat offsetY = 0;

for (NSInteger i = 0; i <= indexPath.row; i++) {
    offsetY += [tableView.delegate tableView:tableView heightForRowAtIndexPath:indexPath];

CGPoint offset = CGPointMake(0, offsetY);


You can also use scrollToRowAtIndexPath:animated: method with animated set to NO in the animation block instead of setContentOffset, and then you don't have to calculate the offset yourself.