Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UITableViewCells between 2 UITableviewCells in an UITableView with an animation

Right now I am making an iPad app that involves a UITableView that involves UITableView Cells that slide left and right, I have a NSNotification based system of letting the UITableViewController know when a slide occurs. I need to display a UITableViewCell under the cell that was moved and above the cell that was below the moved cell. I would like to do a fold animation as seen in the app clear but without the pinch gesture(i.e. automated fold). I have found this library called MPFoldTransition. However, it doesn't support UITableView. I have also looked into the pinch gesture in this tutorial, however this tutorial doesn't tell me how to automatically animate the animation, as it requires the users to do a pinch gesture. I have looked around the web and cant seem to find anything. I will appreciate any help whatsoever.

Thanks

like image 766
virindh Avatar asked Mar 05 '13 00:03

virindh


1 Answers

This is completely possible using MPFoldTransition's transitionFromView: toView:.... I've created a sample project that I can link to if you'd like, but it all really boils down to this:

- (void)addNewTableCellToIndexPath:(NSIndexPath *)indexPath {
    // Add the new object to the datasource and reload the row
    [dataSourceArray insertObject:@"New" atIndex:indexPath.row];

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];

    //setup temporary cell
    UITableViewCell *fromCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [fromCell setBackgroundColor:[[self.tableView cellForRowAtIndexPath:indexPath] backgroundColor]];
    //setup cell to animate to
    UITableViewCell *toCell = [self.tableView cellForRowAtIndexPath:indexPath];
    [toCell.textLabel setText:dataSourceArray[indexPath.row]];

    //add fold animation to the cells create above
    [MPFoldTransition transitionFromView:fromCell
                                  toView:toCell
                                duration:0.4
                                   style:MPFoldStyleUnfold
                        transitionAction:MPTransitionActionNone
                              completion:^(BOOL finished) {
                                  [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                              }];
}

About is a function that takes the index path as an argument. Send it the index path that you wish to add the cell with a fold transition, and it will create a blank temporary cell on the table to fold from, while shifting the rest of the cells down. It isn't perfect, mostly because I haven't figured out a way to change UITableViewRowAnimation's duration to perfectly match up with the folds duration.

Either way, this should be enough to get you going. Hope this helps!

like image 148
Mick MacCallum Avatar answered Oct 27 '22 21:10

Mick MacCallum