0

我正在UILabel使用动画来“蒸发”屏幕CATransition
我希望标签文本变成绿色,然后离开屏幕。

以下代码可以很好地“蒸发”标签,但在制作动画之前不会更改其颜色:

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 = @" ";

调用setNeedsDisplay标签不起作用。
无法使用 a CABasicAnimation,因为标签文本正在更改。

我做错了什么,我该如何解决?

4

1 回答 1

2

您基本上想要嵌套动画,或者在某种意义上,寻找完成块类型的东西。
我能想到的最接近的实现方式是使用CATransition委托

例子:

-(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:@" "];
}
于 2014-07-16T06:30:02.653 回答