Objective c UITableViewCell阴影上的动画不工作
我想在用户单击单元格时创建阴影动画效果,其中单元格的阴影将从0“增长”到预期半径 下面是代码的快照,但我无法将其设置为动画:Objective c UITableViewCell阴影上的动画不工作,objective-c,uitableview,core-animation,Objective C,Uitableview,Core Animation,我想在用户单击单元格时创建阴影动画效果,其中单元格的阴影将从0“增长”到预期半径 下面是代码的快照,但我无法将其设置为动画: -(void) tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [CATransactio
-(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];
}
有什么建议吗?例如,使用
CABasicAnimation
动画,
要仅设置动画,shadowOpacity
u可以尝试下面的代码
-(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;
}
-(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;
}
-(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;
}
要同时设置shadowOpacity
和shadowRadius
u的动画,可以使用下面的代码
-(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;
}
-(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;
}
-(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;
}
要仅设置阴影半径的动画,可以使用以下代码
-(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;
}
-(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;
}
-(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;
}
效果非常相似,没有太大区别
编辑
完成动画后按视图控制器
为此,u可以设置动画代理,并在动画完成后调用该代理
比如说,
-(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];
}
}
谢谢你的代码有效。我还发现了我的问题,因为在上面的代码之后,我有一个navigationController转换动画;通过添加这些,选定UITableViewCell时不会出现阴影动画。你能告诉我应该怎么做,这样我可以设置阴影的动画,然后再设置navigationController过渡动画吗?当我在最后一行中添加此内容时:[self.navigationController pushViewController:_newViewControllerAnimated:YES];然后,我再也看不到阴影动画,屏幕将直接过渡到下一个ViewController。好的,在阴影动画完成后,您想按一下view controller吗?您好。如何在动画结束时删除Groupanimation