UILabel 的背景颜色不可设置动画吗?
我有一个 gridView,我用它来显示几个 GridViewCell : UIView 。 GidViewCell
向自身添加 UILabel,并将 UITapGestureRecognizer
附加到自身。
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
in tapped:
我想播放一些动画,包括更改标签的背景颜色
- (void) tapped:(id)sender {
[UIView animateWithDuration:1.0
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor blueColor]];
}
completion:^(BOOL finished){
[(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor whiteColor]];
}
];
[self.delegate didSelectGridViewCell:self];
}
,但标签只是眨眼间闪烁蓝色 - 如果有的话。 如果我使用相同的代码但调整标签的 alpha 而不是背景颜色,则一切都会按预期工作。
苹果文档将backgroundColor列为可动画的。 是吗?我做错了什么吗?
I have a gridView, I use to display several GridViewCell : UIView
. The GidViewCell
adds an UILabel to itself and attaches anUITapGestureRecognizer
to itself.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
in tapped:
I want to play some animation, including changing the background color of the label
- (void) tapped:(id)sender {
[UIView animateWithDuration:1.0
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor blueColor]];
}
completion:^(BOOL finished){
[(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor whiteColor]];
}
];
[self.delegate didSelectGridViewCell:self];
}
But the label just flashes blue for a blink of an eye — if at all.
If I use the same code but adjust the alpha of the label instead of the background color, everything works as expected.
Apple's docs lists backgroundColor as animatable. Is it? Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己从未尝试过,但我知道有人尝试过。我不知道这是文档中的错误还是代码中的错误。我假设是后者,因为
UILabel
继承自UIView
并且我确信您能够为UIView
的背景设置动画。显然,解决方法是深入到底层 CALayer 并在那里设置背景颜色。I've never tried this myself but I know of someone who has. I don't know if this is a bug in the documentation or in the code. I'm assuming the latter since
UILabel
inherits fromUIView
and I know for sure that you're able to animate the background of aUIView
. The workaround apparently is to drill down to the underlyingCALayer
and set the background color there.