gpt4 book ai didi

ios - UILabel 的图层在动画时不更新颜色

转载 作者:行者123 更新时间:2023-11-28 22:02:42 25 4
gpt4 key购买 nike

我正在使用 CATransitionUILabel 设置动画以“蒸发”出屏幕。
我希望标签文本变为绿色,并移出屏幕。

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

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,因为标签文本正在更改。

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

最佳答案

你基本上想要嵌套动画,或者在某种意义上,寻找一个完成 block 类型的东西。
我能想到的最接近的实现方式是使用 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:@" "];
}

关于ios - UILabel 的图层在动画时不更新颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24724710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com