是UILabel的backgroundColor不可动画吗? [英] Is UILabel's backgroundColor not animatable?

查看:150
本文介绍了是UILabel的backgroundColor不可动画吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridView,我用来显示几个 GridViewCell:UIView GidViewCell 向自己添加一个UILabel,并将 UITapGestureRecognizer 附加到自身。

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];

tapped:动画,包括更改标签的背景颜色

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];
}

但是标签只是闪烁蓝色, 。
如果我使用相同的代码,但调整标签的alpha而不是背景颜色,一切正常工作。

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的文档将backgroundColor列为动画。 是吗?我做错了什么?

Apple's docs lists backgroundColor as animatable. Is it? Am I doing something wrong?

推荐答案

我从来没有试过这个,但我知道有人。我不知道这是否是在文档或代码中的错误。我假设后者,因为 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.

这篇关于是UILabel的backgroundColor不可动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆