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];
4
  • Check out this question: stackoverflow.com/questions/3832474/… It uses CATransaction to achieve what you want :-) Feb 6, 2013 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 6, 2013 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 7, 2014 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, 2014 at 14:37

3 Answers 3

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
}];
6
  • 1
    Works for me on iOS 7 and iOS 8! Apr 29, 2015 at 2:05
  • That's an answer! Works also on iOS 9.
    – silvansky
    Aug 3, 2015 at 12:54
  • Works in ios 9 but makes other animations act weird
    – Miniroo
    Mar 15, 2016 at 20:48
  • @Miniroo maybe there is something else which is wrong with there. The code works on iOS9. Are you sure you are removing the cell correctly? Can you help with your code so I can suggest appropriately. Mar 31, 2016 at 12:17
  • 2
    The animation duration is ignored for me.
    – Dustin
    Aug 25, 2016 at 18:37
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
    // ...
}
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];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.