【问题标题】:UILabel's layer doesn't update color while animatingUILabel 的图层在动画时不更新颜色
【发布时间】:2014-09-03 16:07:09
【问题描述】:

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

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

【问题讨论】:

  • 为什么这被否决了?
  • 让我们看看文档是怎么说的,经过一分钟的研究:"UIView 类的以下属性是可动画的:frameboundcenter、@ 987654330@,alphabackgroundColorcontentStretch"...也许这就是为什么你不能为textColor设置动画,也许?
  • @holex 我们在这里讨论的是使用 CoreAnimation,而不是基于 UIView 的动画。 AFAIK,CoreAnimation 与内容无关。我只希望 CA 在 textColor 更改并且图层更新之后为标签设置动画。文档还说CALayercontents 属性是可动画的。

标签: ios objective-c cocoa-touch core-animation catransition


【解决方案1】:

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

【讨论】:

  • 谢谢。我今晚会试试这个并报告。
  • @duci9y:当然。嗯......没有特定的“蒸发”类型动画,但我认为这就是你想要的。
  • 非常感谢。虽然无法完全达到我想要的效果,但目前有效。
  • @duci9y :不客气。顺便说一句...你想要的效果,是让文本在 向上移动时变成绿色吗?
  • 不行,变成绿色,然后向上移动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 2015-11-14
  • 2021-07-16
  • 1970-01-01
  • 2013-12-01
  • 2013-09-10
相关资源
最近更新 更多