19

Is there a way to change the duration of [table beginUpdates]/[table endUpdates] animations?

This is what I've tried, with no luck:

Option 1:

[UIView animateWithDuration:5.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionOverrideInheritedDuration) animations:^{

     [self.tableView beginUpdates];

     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:indexPaths] withRowAnimation:UITableViewRowAnimationTop];

     [self.tableView endUpdates];


} completion:^(BOOL finished) {
}];

Option 2:

[CATransaction begin];

[CATransaction setCompletionBlock:^{
    NSLog(@"I actually get called!");
}];

[CATransaction setAnimationDuration:5.0]; //but I don't work

[self.tableView beginUpdates];

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:indexPaths] withRowAnimation:UITableViewRowAnimationTop];

[self.tableView endUpdates];

[CATransaction commit];
Tom Redman
  • 5,592
  • 4
  • 34
  • 42
  • Check out this question: http://stackoverflow.com/questions/3832474/uitableview-row-animation-duration-and-completion-callback/13041475#13041475 It uses CATransaction to achieve what you want :-) – Ben Clayton Feb 06 '13 at 16:17
  • I've seen that question. It would work, but my problem is I need the duration specifically, which doesn't work. I.e., in option 2 in this question, I need the line `[CATransaction setAnimationDuration:5.0];` to work and it doesn't. – Tom Redman Feb 06 '13 at 16:31
  • @TomRedman ever get a good solution for this problem? I see in the question linked to by Ben above that there now seems to be one that works in iOS 7, but not 6 ... – Will Moore Feb 07 '14 at 17:10
  • @WillMoore, no we've not found a way to reliably modify a tableView's animation duration. I'll have to revisit and see if the duration property solution does work on iOS 7. – Tom Redman May 14 '14 at 14:37

3 Answers3

16

Why don't you try UIView animation.

[UIView animateWithDuration:2 delay:0.2 options:UIViewAnimationOptionCurveEaseInEaseOut animations:^{
  [self.tableView beginUpdates];
  [self.tableView endUpdates];
} completion:^(BOOL finished) {
  // code
}];
Gautam Jain
  • 2,913
  • 30
  • 25
5

Here is the Swift version of Gautam Jain's answer :

UIView.animate(withDuration: 2.0, delay: 0.0, options: .curveEaseInOut, animations: {
    self.tableView.beginUpdates()
    // ...
    self.tableView.endUpdates()
}) { isFinished in
    // ...
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
3

@Gautam Jain 's solution is great. However, it has a problem, at least in iOS 9: the completion block will be executed at once but not when the animation completes.

I usually do like below, with a little more code but works better.

[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:0.25];
[CATransaction begin];
[CATransaction setCompletionBlock:^{
   // completion block
}];

[self.tableView beginUpdates];
// updates  
[self.tableView endUpdates];

[CATransaction commit];
[UIView commitAnimations];
Igor
  • 12,165
  • 4
  • 57
  • 73
lxmfly123
  • 126
  • 9