2016-04-11 23 views
1

Kullanıcı, hücreyi tıklattığında, hücrenin, 0'dan istenen yarıçapa "büyüyen" bir gölgeye sahip olacağı bir gölge animasyonu efekti oluşturmak istedim.UITableViewCell gölgesinde animasyon çalışmıyor

Aşağıda kodlarının anlık görüntüleridir ama animasyon alamıyorum:

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

Herhangi bir tavsiye?

cevap

2

kullanımı CABasicAnimation animasyon yerine, örneğin,

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

animasyon

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

hem shadowOpacity ve shadowRadius U aşağıdaki kodu kullanabilir animasyon u kodu altında deneyin sadece shadowOpacity animasyon yalnızca shadowRadius yalnızca aşağıdaki kodu kullanabilirsiniz

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

etkisi Düzenleme

u animasyon temsilci ayarlayabilirsiniz Bunun için bitirmek sonra animasyonu görünüm denetleyicisi Pushing ve animasyon bittikten sonra denir, çok farklı oldukça benzer değildir.

örneğin

,

-(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

teşekkürler! Kodların çalışır. Sorunumu da buldum, çünkü yukarıdaki kodlardan hemen sonra bir navigationController geçiş animasyonum var; ve bunları ekleyerek, UITableViewCell seçildiğinde gölge animasyonu görünmez. Yapmam gereken şeyi öneriyorum, böylece gölgeyi canlandırabilir ve ardından navigationController geçiş animasyonu izleyebilir miyim? – Calvin

+0

Bunu son satıra eklediğimde: [self.navigationController pushViewController: _newViewController animated: YES]; daha sonra, gölge animasyonunu artık göremiyorum ve ekran bir sonraki ViewController'a doğrudan geçiş yapacak. – Calvin

+0

Tamam, gölge animasyonu tamamlandıktan sonra görüntü denetleyicisini zorlamak ister misiniz ..? –