Verwendung CABasicAnimation
Animation statt, zum Beispiel zu animieren nur, shadowOpacity
u Code unten versuchen
-(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;
}
beide shadowOpacity
und shadowRadius
animieren u unterhalb Code
-(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;
}
verwenden können, um zu animieren Nur shadowRadius
Sie können den folgenden Code verwenden
-(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;
}
die Wirkung ist ganz ähnlich nicht zu unterschiedlich,
bearbeiten Drücken der View-Controller nach dem Beenden der Animation Dazu u die Animation Delegierten festlegen und es wird aufgerufen, nachdem die Animation beendet.
zum Beispiel
-(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];
}
}
Dank! Deine Codes funktionieren. Ich habe auch mein Problem gefunden, denn direkt nach den obigen Codes habe ich eine navigationController-Übergangsanimation; Wenn Sie diese hinzufügen, wird die Schattenanimation nicht angezeigt, wenn UITableViewCell ausgewählt ist. Kannst du mir raten, was ich tun soll, damit ich den Schatten animieren kann und dann von der navigationController Transition Animation gefolgt wird? – Calvin
Wenn ich das in die letzte Zeile eingefügt habe: [self.navigationController pushViewController: _newViewController animiert: YES]; dann kann ich die Schattenanimation nicht mehr sehen und der Bildschirm wird direkt zum nächsten ViewController übergehen. – Calvin
okay, Sie möchten den View-Controller drücken, nachdem die Schattenanimation abgeschlossen ist ..? –