Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView row animation duration and completion callback

Is there a way to either specify the duration for UITableView row animations, or to get a callback when the animation completes?

What I would like to do is flash the scroll indicators after the animation completes. Doing the flash before then doesn't do anything. So far the workaround I have is to delay half a second (that seems to be the default animation duration), i.e.:

[self.tableView insertRowsAtIndexPaths:newRows                       withRowAnimation:UITableViewRowAnimationFade]; [self.tableView performSelector:@selector(flashScrollIndicators)                      withObject:nil                      afterDelay:0.5]; 
like image 604
Daniel Dickison Avatar asked Sep 30 '10 16:09

Daniel Dickison


2 Answers

Just came across this. Here's how to do it:

Objective-C

[CATransaction begin]; [tableView beginUpdates]; [CATransaction setCompletionBlock: ^{     // Code to be executed upon completion }]; [tableView insertRowsAtIndexPaths: indexPaths                  withRowAnimation: UITableViewRowAnimationAutomatic]; [tableView endUpdates]; [CATransaction commit]; 

Swift

CATransaction.begin() tableView.beginUpdates() CATransaction.setCompletionBlock {     // Code to be executed upon completion } tableView.insertRowsAtIndexPaths(indexArray, withRowAnimation: .Top) tableView.endUpdates() CATransaction.commit() 
like image 104
karwag Avatar answered Sep 21 '22 08:09

karwag


Expanding on karwag's fine answer, note that on iOS 7, surrounding the CATransaction with a UIView Animation offers control of the table animation duration.

[UIView beginAnimations:@"myAnimationId" context:nil];  [UIView setAnimationDuration:10.0]; // Set duration here  [CATransaction begin]; [CATransaction setCompletionBlock:^{     NSLog(@"Complete!"); }];  [myTable beginUpdates]; // my table changes [myTable endUpdates];  [CATransaction commit]; [UIView commitAnimations]; 

The UIView animation's duration has no effect on iOS 6. Perhaps iOS 7 table animations are implemented differently, at the UIView level.

like image 25
Brent Avatar answered Sep 21 '22 08:09

Brent