首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >UITableViewCell阴影上的动画无法工作

UITableViewCell阴影上的动画无法工作
EN

Stack Overflow用户
提问于 2016-04-10 19:22:03
回答 1查看 403关注 0票数 2

当用户单击该单元格时,我希望创建一个阴影动画效果,在该单元格中,阴影将从0“增长”到预定的半径。

下面是代码的快照,但我无法将其动画化:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void) tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [CATransaction begin]; {
        [CATransaction setAnimationDuration:5];
        cell.layer.shadowOpacity = 1.0;
        cell.layer.shadowRadius = 20;
        cell.layer.shadowColor = [UIColor blackColor].CGColor;
        cell.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    }
    [CATransaction commit];

    [tableView_ deselectRowAtIndexPath:indexPath animated:YES];
}

-(void) tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    CABasicAnimation  *animShadow = [CABasicAnimation animationWithKeyPath:@"shadowRadius"];
    animShadow.fromValue = [NSNumber numberWithFloat:0.0];
    animShadow.toValue = [NSNumber numberWithFloat:20];
    animShadow.duration = 3.0;
    [cell.layer addAnimation:animShadow forKey:@"shadowRadius"];

    [tableView_ deselectRowAtIndexPath:indexPath animated:YES];
}

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-10 22:35:23

使用CABasicAnimation动画代替,例如,为了只动画,shadowOpacity u可以尝试下面的代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    //animation  shadowOpacity
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat:1.0];
    animation.duration = 1.0f;

    [cell.layer addAnimation:animation forKey:@"shadowOpacityAnimation"];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    cell.layer.shadowOpacity = 1.0f;
    cell.layer.shadowRadius = 20.0f;
 }

要动画化shadowOpacityshadowRadius,您可以使用下面的代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //animation both the shadowOpacity and shadowRadius
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
   animation.fromValue = [NSNumber numberWithFloat:0.0];
   animation.toValue = [NSNumber numberWithFloat:1.0];

   CABasicAnimation *shadowRadiusAnimation = [CABasicAnimation animationWithKeyPath:@"shadowRadius" ];
   shadowRadiusAnimation.fromValue =[NSNumber numberWithFloat:0.0];
   shadowRadiusAnimation.toValue = [NSNumber numberWithFloat:20.0];

   CAAnimationGroup *group = [CAAnimationGroup animation];
   group.animations = @[animation,shadowRadiusAnimation];
   group.duration = 1.0f;

   [cell.layer addAnimation:group forKey:@"shadowGroupAnimation"];
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
   cell.layer.shadowOpacity = 1.0f;
   cell.layer.shadowRadius = 20.0f;
}

要动画化只有shadowRadius,您可以使用下面的代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //animation  shadowRadius
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


  CABasicAnimation *shadowRadiusAnimation = [CABasicAnimation animationWithKeyPath:@"shadowRadius" ];
  shadowRadiusAnimation.fromValue =[NSNumber numberWithFloat:0.0];
  shadowRadiusAnimation.toValue = [NSNumber numberWithFloat:20.0];
shadowRadiusAnimation.duration = 1.0f;

  [cell.layer addAnimation:shadowRadiusAnimation forKey:@"shadowRadiusAnimation"];
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  cell.layer.shadowOpacity = 1.0f; //set final values
  cell.layer.shadowRadius = 20.0f;
}

效果非常相似,没有太大的不同,

编辑在动画完成后推送视图控制器,可以设置动画委托,动画完成后调用。

例如,

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //animation both the shadowOpacity and shadowRadius
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
   animation.fromValue = [NSNumber numberWithFloat:0.0];
   animation.toValue = [NSNumber numberWithFloat:1.0];

   CABasicAnimation *shadowRadiusAnimation = [CABasicAnimation animationWithKeyPath:@"shadowRadius" ];
   shadowRadiusAnimation.fromValue =[NSNumber numberWithFloat:0.0];
   shadowRadiusAnimation.toValue = [NSNumber numberWithFloat:20.0];

   CAAnimationGroup *group = [CAAnimationGroup animation];
   group.delegate = self; //this line set the delegate to self,
   //if u use other option's also u can set the delegate for 
   //"CABasicAnimation" also just set it to self and implement the
   // delegate method
   group.animations = @[animation,shadowRadiusAnimation];
   group.duration = 1.0f;
   [cell.layer addAnimation:group forKey:@"shadowGroupAnimation"];
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
   cell.layer.shadowOpacity = 1.0f;
   cell.layer.shadowRadius = 20.0f; 
}


//implement the delegate method this is called when animation stops with the flag,

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
 {
   if(flag)
   {
     SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
     [self.navigationController pushViewController:controller animated:YES];
  }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36539307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文