Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of animating a UITableViewCell so that the row flashes briefly to a selected state and then back to white on selection?

I just want the cell to briefly highlight either with a UIColor or a background image before animating back to the unselected state when a user taps the cell. I'm marking the cell with a star to show that it has been selected but would like the whole cell to highlight briefly just to show selection.

like image 208
Nathan Avatar asked Feb 07 '11 21:02

Nathan


2 Answers

add this line in the didSelectRowAtIndexPath -

[tableView deselectRowAtIndexPath:indexPath animated:YES];
like image 80
shannoga Avatar answered Nov 04 '22 23:11

shannoga


I think the answer provided is solid if you want to animate just one row in response to selection.

I was looking for a way to do this with multiple rows simultaneously and without selecting and deselecting the rows in response to touch. I did the following:

Created a method in my UITableViewController called "calloutCells":

- (void)calloutCells:(NSArray*)indexPaths
{   
    [UIView animateWithDuration:0.0 
                          delay:0.0 
                        options:UIViewAnimationOptionAllowUserInteraction 
                     animations:^void() {
                         for (NSIndexPath* indexPath in indexPaths)
                         {
                             [[self.tableView cellForRowAtIndexPath:indexPath] setHighlighted:YES animated:YES];
                         }
                      } 
                     completion:^(BOOL finished) {
                         for (NSIndexPath* indexPath in indexPaths)
                         {
                             [[self.tableView cellForRowAtIndexPath:indexPath] setHighlighted:NO animated:YES];
                         }
                      }];
}

Note that the duration and delay don't change the animation I've chosen since I'm using the built in animation on setHighlighted.

When I want to initiate the highlight/unhighlight animation from the UITableViewController, I can simultaneously highlight rows 3 and 4 of section 0 like this:

[self calloutCells:[NSArray arrayWithObjects:
                     [NSIndexPath indexPathForRow:3 inSection:0], 
                     [NSIndexPath indexPathForRow:4 inSection:0], nil]];

Hope this helps someone else!

like image 14
Pat Avatar answered Nov 04 '22 21:11

Pat