I am animating a UILabel
to 'evaporate' off the screen using CATransition
.
I want the label text to turn green, and move off the screen.
The following code does 'evaporate' the label fine, but doesn't change its colour before animating:
CATransition *transition = [CATransition animation];
transition.beginTime = CACurrentMediaTime();
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
[self.displayLabel.layer addAnimation:transition forKey:@"evaporate"];
self.displayLabel.textColor = [self greenColor];
self.displayLabel.text = @" ";
Calling setNeedsDisplay
on the label doesn't work.
Can't use a CABasicAnimation
because the label text is changing.
What am I doing wrong and how do I fix it?
You basically want nested animations or, in a sense, looking for a completion block type thing.
The closest I can think of achieving this is by using the CATransition
delegate
-(IBAction)btnTest:(UIButton *)sender
{
//[1] Animate text color change
CATransition *animation = [CATransition animation];
[animation setDelegate:self]; //important
[animation setRemovedOnCompletion:YES]; //better to remove the animation
[animation setBeginTime:CACurrentMediaTime()]; //not really needed
[animation setDuration:0.4];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[animation setType:kCATransitionFade];
//you can specify any key name or keep it nil. doesn't make a difference
[self.displayLabel.layer addAnimation:animation forKey:@"changeTextColor"];
[self.displayLabel setTextColor:[UIColor greenColor]];
}
#pragma mark - CATransition Delegate
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//[2] Animate text sublayer
/*
The following CATransition should not set a delegate
otherwise this animation will loop continously
*/
CATransition *animation = [CATransition animation];
[animation setRemovedOnCompletion:YES]; //better to remove the animation
[animation setBeginTime:CACurrentMediaTime()]; //not really needed
[animation setDuration:0.4];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromTop];
//you can specify any key name or keep it nil. doesn't make a difference
[self.displayLabel.layer addAnimation:animation forKey:@"changeText"];
[self.displayLabel setText:@" "];
}