UILabel 的背景颜色不可设置动画吗?

发布于 2024-11-16 10:57:50 字数 1357 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-11-23 10:57:50

我自己从未尝试过,但我知道有人尝试过。我不知道这是文档中的错误还是代码中的错误。我假设是后者,因为 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 from UIView and I know for sure that you're able to animate the background of a UIView. The workaround apparently is to drill down to the underlying CALayer and set the background color there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文