3

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

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];

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 而不是背景颜色,一切都会按预期工作。

Apple 的文档将 backgroundColor 列为可动画的。是吗?难道我做错了什么?

4

1 回答 1

11

我自己从未尝试过,但我知道有人尝试过。我不知道这是文档还是代码中的错误。我假设是后者,因为UILabel继承自UIView并且我确信您能够为 a 的背景设置动画UIView。解决方法显然是深入到底层CALayer并在那里设置背景颜色。

于 2011-06-22T15:55:35.867 回答