【问题标题】:What is actually animatable in UILabel?UILabel 中实际上可以动画的内容是什么?
【发布时间】:2010-10-25 18:42:30
【问题描述】:

我正在尝试为 UILabel 类的 backgroundColor 属性设置动画,但到目前为止还没有成功。这是我的代码的 sn-p

-(void) blink {
  UIColor* originalColor = lblDescription.backgroundColor;
  lblDescription.backgroundColor = [UIColor yellowColor];

  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:1.0];
  lblDescription.backgroundColor = originalColor;
  [UIView commitAnimations];
}
//this code works if lblDescription is UIView and does not if UILabel

我发现有人声称某些 UILabel 属性不可动画化,但我无法通过阅读 Apple 文档来证实这一说法。我想知道是否有人可以阐明这个问题。

【问题讨论】:

  • 好问题。大多数时候我只是想知道它是否可以动画......

标签: iphone cocoa-touch uikit core-animation


【解决方案1】:

在this page from the View Programming Guide For iOS 上,“表 1-2 动画属性”应该列出了 UIView 的所有动画属性。它不包括背景颜色。

但是在 UIView 类参考中,在 backgroundColor 属性下它说:

讨论
对此属性的更改可以设置动画。默认值为 nil。

所以不清楚为什么 UILabel 的 backgroundColor 没有动画。

我过去使用的解决方法是使用 CATextLayer 而不是 UILabel。

【讨论】:

  • 在this question 中尝试这两种其他解决方法。这些应该可以在早期的操作系统版本中使用。
  • @aBitObvious CATextLayer 没有垂直对齐功能(在 iOS 中)并在其表面顶部呈现文本(UILabel 在中心呈现)。我放弃了:(
  • @bioffe:使用两个 UILabel 相互重叠的解决方法是一种简单的解决方法。你试过了吗?有重复的 UILabel 有点烦人,但是你只是在相反的方向上为它们的 alpha 设置动画。
  • @aBitObvious 这正是我接下来要做的。同时,我向 Apple 提交了这个毫无希望的错误问题。感谢您的帮助。
  • 这是一个更好的解决方法,只使用原始 UILabel 本身:stackoverflow.com/questions/6442774/…
【解决方案2】:

如果您想为标签(包括背景)的颜色设置动画,您可以执行以下操作:

https://stackoverflow.com/a/20892927/189924

这不涉及层或使用多个视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 2011-08-01
    • 2011-12-26
    • 1970-01-01
    • 2017-03-16
    • 2022-12-16
    • 2012-11-16
    • 2011-03-11
    相关资源
    最近更新 更多