繁体   English   中英

动画时UILabel的图层不会更新颜色

[英]UILabel's layer doesn't update color while animating

我正在对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不起作用。
标签文本正在更改,因此无法使用CABasicAnimation

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

基本上,您需要嵌套动画,或者从某种意义上讲,是在寻找完成块类型的东西。
我能想到的最接近的方法是使用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:@" "];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM