Search code examples
iphoneobjective-ccore-animation

Animations not happening on my UILabel


I wrote some animation for some images, and it works. Its pretty much the same animation again but this time on a UILable. But nothing seems to have. The label draws but when i call the animation method the text does not change/move.

-(void) bounceText
{

NSLog(@"Bounce Text");
CABasicAnimation *grow;
grow = [CABasicAnimation animationWithKeyPath:@"growText"];
grow.timingFunction = [CAMediaTimingFunction 
    functionWithName:kCAMediaTimingFunctionLinear];
grow.toValue = [NSNumber numberWithFloat:3.0];
grow.fromValue = [NSNumber numberWithFloat:0.1];
grow.repeatCount = 10;
grow.fillMode = kCAFillModeForwards; 
grow.removedOnCompletion = YES;
grow.duration = 5.0;
grow.autoreverses = NO;
grow.delegate = self;


CABasicAnimation *fade;
fade = [CABasicAnimation animationWithKeyPath:@"fade"];
fade.fromValue = [NSNumber numberWithFloat:0.5];
fade.toValue = [NSNumber numberWithFloat:1.0];
fade.duration = 5.0;
CALayer *layer = [CALayer layer];
hintsLabel.layer.transform=CATransform3DTranslate(CATransform3DIdentity, 0, 0,50);   

[CATransaction begin];
[hintsLabel.layer addSublayer:layer];
[hintsLabel.layer addAnimation:grow forKey:@"growTheText"];
[layer addAnimation:fade forKey:@"fadeText"];
[CATransaction commit]; 
}

Calling of the method and adding text to the label

 -(void) drawHints
 {
if (gameState == SHOWCARD)
{
    hintsLabel.layer.zPosition = 5;
    hintsLabel.text = @"It's your turn, select a button!";
    if (!bounce)
    {
    [self bounceText];
        bounce = YES;   
    }
}
}

Is it that labels can not be transformed?

Been playing with this for an hour and nothing is happening -.-

Thanks -Code


Solution

  • The main issue here is that you're not specifying the properties to animate. The documentation for CAPropertyAnimation states the following for the animationWithKeyPath: method:

    The key path of the property to be animated.

    I'm not 100% sure what you're trying to achieve with your animation but where you have the following code:

    fade = [CABasicAnimation animationWithKeyPath:@"fade"];
    

    that should be:

    fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
    

    because you want to affect the label's opacity to make it fade in or out. So to make your label fade in from an opacity of 0.5 to 1.0 over 5 seconds, you would use the following code:

        CABasicAnimation *fade;
    fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fade.fromValue = [NSNumber numberWithFloat:0.5];
    fade.toValue = [NSNumber numberWithFloat:1.0];
    fade.duration = 5.0;
    
    [hintsLabel.layer addAnimation:fade forKey:@"fade"];