Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change duration of UITableView animation (Insert/Delete rows with table beginUpdates)

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];
like image 313
Tom Redman Avatar asked Feb 06 '13 16:02

Tom Redman


3 Answers

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
}];
like image 190
Gautam Jain Avatar answered Nov 01 '22 10:11

Gautam Jain


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
    // ...
}
like image 32
Ahmad F Avatar answered Nov 01 '22 11:11

Ahmad F


@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];
like image 3
lxmfly123 Avatar answered Nov 01 '22 09:11

lxmfly123