1

When the UITableView loads I want its first cell to bounce from the Right side, so It indicates the User that they can swipe right to delete a cell.

How can I do this ?

My code so far:

note: In the following code I am just making the cell to blink, but what I actually want is the cell to bounce.

-(void) tableView:(UITableView *) tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //row number on which you want to animate your view
    //row number could be either 0 or 1 as you are creating two cells
    //suppose you want to animate view on cell at 0 index
    if(indexPath.row == 0) //check for the 0th index cell
    {
        // access the view which you want to animate from it's tag
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:1];

        UIView *myView = [self.tableView cellForRowAtIndexPath:indexPath];
        NSLog(@"row %ld",(long)indexPath.row);

        // apply animation on the accessed view
        [UIView animateWithDuration:5
                              delay:2
                            options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^
         {
             [myView setAlpha:0.0];
         } completion:^(BOOL finished)
         {
             [myView setAlpha:1.0];
         }];
    }
}
Illep
  • 16,375
  • 46
  • 171
  • 302

1 Answers1

0

If I understand this correctly, you wish to bounce the cell from left to right?

Get a reference to the cell's contentView:

 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:1];

 UIView *contentView = [self.tableView cellForRowAtIndexPath:indexPath].contentView;

Now there are many ways to 'bounce' this contentView. One way would be to use an animation block like this:

CGRect original = contentView.frame;
CGRect bounce_offset = original;
original.origin.x -= 100.0f;

What we do here, is remember the original frame, and decide how far we want our bounce to reach in the animation block. Then we can animate for example doing something like this:

[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    contentView.frame = bounce_offset;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:1.0f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
        contentView.frame = original;
    } completion:^(BOOL finished) {

    }];
}]

You could also use autoreverse options, this 'a' way to do it though. Let me know what your thoughts are!

trdavidson
  • 1,051
  • 12
  • 25