如何淡化UIView背景色 - iOS 5 [英] How to fade UIView background color - iOS 5

查看:122
本文介绍了如何淡化UIView背景色 - iOS 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试获取自定义UIView类以淡化它的背景时遇到了麻烦。我查看了以下StackOverflow问题,但它们似乎对我不起作用。

I'm having trouble trying to get a custom UIView class to fade it's background. I've checked out the following StackOverflow questions but they don't seem to work for me.

在UIView中将背景颜色从一种颜色淡化为另一种颜色

如何明确动画CALayer的backgroundColor?

所以我有一个自定义UIView,用户可以在其中绘制内容。如果他们绘制的内容不正确,我想让背景颜色渐变为红色然后再回到白色。

So I have a custom UIView where users can draw things. If what they draw is incorrect, I want to make the background color fade to red then back to white.

我在自定义UIView中有这个自定义方法叫

I have this custom method in the custom UIView called

- (void)indicateMistake;

当我调用该方法时,我希望它执行背景色动画,所以我在方法:

When I call that method I want it to perform the background color animation, so i have this in the method:

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

但是当我这样做时,似乎没有任何事情发生。

But nothing seems to happen when I do that.

然后我尝试了一个愚蠢的旋转动画,看看它是否有效:

So then I tried a silly rotation animation to see if it even works:

CABasicAnimation* rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.toValue = [NSNumber numberWithInt:M_PI];
[rotate setDuration:1];
[self.layer addAnimation:rotate forKey:@"rotateAnimation"];

出于某种原因,自定义UIView旋转。

For some reason that works and the custom UIView rotates.

然后阅读更多StackOverflow答案我试过这个:

Then reading more StackOverflow answers I tried this:

[UIView animateWithDuration:3 animations:^{
    [self setBackgroundColor: [UIColor redColor]];
} completion:^(BOOL finished) {
    [self setBackgroundColor: [UIColor whiteColor]];
}];

这会立即将颜色从红色变回白色。有时它太快我有时看不到它发生。如果我注释掉 [self setBackgroundColor:[UIColor whiteColor]]; 它会保持红色。但是节点会逐渐变为白色到红色的效果。

That changes the color from red then back to white immediately. Sometimes It's so fast I sometimes can't see it happen. If i comment out the [self setBackgroundColor: [UIColor whiteColor]]; it stays red. But There's node gradual white to red effect.

我的想法已经用完了......任何帮助都会受到赞赏!

I've ran out of ideas.. Any help would be appreciated!

推荐答案


所以我有一个自定义UIView,用户可以在这里绘制东西。

So I have a custom UIView where users can draw things.

我认为这意味着你已经实现了 -drawRect:

I'm assuming that means you've implemented -drawRect:.

IIRC,默认情况下(如果设置了 UIView.clearsContextBeforeDrawing ),上下文将填充视图的背景颜色, -drawRect:被调用,并在屏幕上绘制结果位图而不是背景颜色。动画这将意味着两个位图之间的动画,这不是Core Animation特别擅长的。 (我在这里的细节可能是错的,但它解释了行为)。

IIRC, by default (if UIView.clearsContextBeforeDrawing is set), the context gets filled with the view's background colour, -drawRect: gets called, and the resulting bitmap is drawn on screen instead of the background colour. Animating this would mean animating between the two bitmaps, which isn't something Core Animation is particularly good at. (I could be wrong about the details here, but it explains the behaviour).

最简单的解决办法是将背景颜色设置为 [UIColor clearColor] 并为您要绘制的视图后面的视图的背景颜色设置动画。这意味着视图不需要重新绘制,只需在背景上重新编译。

The easiest fix is to set the background colour to [UIColor clearColor] and animate the background colour of a view behind the one you are drawing to. This means the view will not need to be redrawn, only recomposited over the "background".

这篇关于如何淡化UIView背景色 - iOS 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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