Search code examples
iosobjective-cuitableviewcore-animation

shaking tableviewcell in iOS


I am trying to Animate the cell when did select the row at indexPath and I am succeeded with this code in didSelectRowAtIndexpath method

TableViewCell1 *cell = (TableViewCell1 *)[tableView cellForRowAtIndexPath:indexPath];

CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
[shake setDuration:0.1];
[shake setRepeatCount:3];
[shake setAutoreverses:YES];
[shake setFromValue:[NSValue valueWithCGPoint:
                     CGPointMake(cell.center.x - 5,cell.center.y)]];
[shake setToValue:[NSValue valueWithCGPoint:
                   CGPointMake(cell.center.x + 5, cell.center.y)]];
[cell.layer addAnimation:shake forKey:@"position"];

but in storyBoard with segue method this Animation Was Disappear, and when I clicked the row it will directly go to next view.

my requirement is when I clicked row the particular row should shake 3 themes and after that it go to next view. please any one Help me...


Solution

  • i think you created segue to the next view directly from table view cell,remove that segue and connect from current view to next view,set identifier to your segue and then code as following

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
        CABasicAnimation* shake  = [CABasicAnimation animationWithKeyPath:@"position"];
        [shake setDuration:0.1];
        [shake setRepeatCount:3];
        [shake setAutoreverses:YES];
    
        [shake setFromValue:[NSValue valueWithCGPoint:
                             CGPointMake(cell.center.x - 5,cell.center.y)]];
        [shake setToValue:[NSValue valueWithCGPoint:
                           CGPointMake(cell.center.x + 5, cell.center.y)]];
        [cell.layer addAnimation:shake forKey:@"position"];
    
        [NSTimer scheduledTimerWithTimeInterval:0.3
                                         target:self
                                       selector:@selector(loadNextView)
                                       userInfo:nil
                                        repeats:NO];
    
    }
    -(void)loadNextView
    {
        [self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];
    }