Question

What is intended: A button click requests that a CATextLayer object's string value is animated until being stopped by another button click request. There are two separate buttons responsible for these actions so that the actions are not confused.

What actually happens: On the play request, there are several instances when the textLayer's string is not animated, and its string value remains unchanged, while displaying the assigned "0" (as shown above). In this case, the animation is never initialized and assigned to the CATextLayer object. However, the error is not consistent. There are also several instances when the CATextLayer object will display the appropriate string values. If continuously clicking the button, the success rate is roughly 70% of seeing the CATextLayer object animate its string value. Are there any scenarios which would bypass the the addAnimation method line of code?

I appreciate any and all of your help with issue. Thanks in advance!

-(IBAction)playText:(id)sender{
    //textLayer object is instantiated elsewhere in the class
    textLayer.frame = CGRectMake(0, 0, 128, 16);
    textLayer.fontSize = 14;
    textLayer.backgroundColor = [UIColor clearColor].CGColor;
    textLayer.foregroundColor = [UIColor yellowColor].CGColor;
    textLayer.string = @"0";
    [self.layer addSubLayer:textLayer];

    CAKeyframeAnimation *textAnimation = [CAKeyframeAnimation animationWithKeyPath:@"string"];
    textAnimation.values = values; 
    textAnimation.repeatCount = HUGE_VALF;
    textAnimation.keyTimes = intervals; 
    textAnimation.calculationMode = kCAAnimationLinear;
    textAnimation.duration = 6;
    [textLayer addAnimation:textAnimation forKey:@"string"];
}

-(IBAction)stopText:(id)sender{
    [textLayer removeAnimationForKey:@"string"];
}
Was it helpful?

Solution

The object must be re-initialized for each keyframe animation, in addition to be re-added to it's parent Sublayer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top