2016-04-11 17 views
1

Ich wollte einen Schatten Animationseffekt erstellen, wenn der Benutzer auf die Zelle klickt, wo die Zelle einen Schatten "wachsen" von 0 bis zum beabsichtigten Radius.Animation auf UITableViewCell Schatten funktioniert nicht

Im Folgenden sind die Schnappschüsse der Codes, aber ich kann es nicht zu animieren erhalten:

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

Jede Beratung?

Antwort

2

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]; 
    } 
} 
+0

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

+0

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

+0

okay, Sie möchten den View-Controller drücken, nachdem die Schattenanimation abgeschlossen ist ..? –

Verwandte Themen