在iOS中摇动uitableviewcell

时间:2014-07-28 11:36:25

标签: ios objective-c uitableview core-animation

我正在尝试动画cell,当我在indexPath选择行时,我在didSelectRowAtIndexpath方法

中成功使用了此代码
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"];

但在使用segue方法的storyBoard中,此动画消失了,当我点击该行时,它将直接转到下一个视图。

我的要求是当我点击行时,特定行应该摇动3个主题,然后进入下一个视图。请任何人帮助我......

2 个答案:

答案 0 :(得分:3)

我认为您直接从表格视图单元格创建了segue到下一个视图,删除了该segue并从当前视图连接到下一个视图,将标识符设置为您的segue然后编码如下

-(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];
}

答案 1 :(得分:0)

-(void)getCellFocused:(int)row table:(UITableView*)table{



     [UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{


            UITableViewCell *cell = [table cellForRowAtIndexPath:inedexPath];

            CAKeyframeAnimation * anim = [ CAKeyframeAnimation animationWithKeyPath:@"transform" ] ;

            anim.values = @[ [ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-10.0f, 0.0f, 0.0f) ], [ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f) ] ] ;

            anim.autoreverses = YES ;

            anim.repeatCount = 2.0f ;

            anim.duration = 0.1f ;



            [ cell.layer addAnimation:anim forKey:nil ] ;



        } completion:nil];

 }