【发布时间】:2013-01-27 00:49:36
【问题描述】:
我在尝试让自定义 UIView 类淡化其背景时遇到问题。我检查了以下 StackOverflow 问题,但它们似乎对我不起作用。
Fade background-color from one color to another in a UIView
How do you explicitly animate a CALayer's backgroundColor?
所以我有一个自定义 UIView,用户可以在其中绘制东西。如果他们画的不正确,我想让背景颜色变红然后变回白色。
我在自定义 UIView 中有这个自定义方法调用
- (void)indicateMistake;
当我调用该方法时,我希望它执行背景颜色动画,所以我在方法中有这个:
CABasicAnimation* fade = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
fade.fromValue = (id)[UIColor whiteColor].CGColor;
fade.toValue = (id)[UIColor redColor].CGColor;
[fade setDuration:3];
[self.layer addAnimation:fade forKey:@"fadeAnimation"];
但是当我这样做时似乎什么都没有发生。
然后我尝试了一个愚蠢的旋转动画,看看它是否有效:
CABasicAnimation* rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.toValue = [NSNumber numberWithInt:M_PI];
[rotate setDuration:1];
[self.layer addAnimation:rotate forKey:@"rotateAnimation"];
由于某些原因,自定义 UIView 会旋转。
然后阅读更多 StackOverflow 答案我尝试了这个:
[UIView animateWithDuration:3 animations:^{
[self setBackgroundColor: [UIColor redColor]];
} completion:^(BOOL finished) {
[self setBackgroundColor: [UIColor whiteColor]];
}];
这会立即将颜色从红色变为白色。有时它是如此之快,我有时看不到它发生。如果我注释掉[self setBackgroundColor: [UIColor whiteColor]];,它会保持红色。但是有节点渐变白到红的效果。
我的想法已经用完了。任何帮助将不胜感激!
【问题讨论】:
标签: ios uiview calayer uibackgroundcolor