使用drawRect设置UIView backgroundColor: [英] Set UIView backgroundColor with drawRect:

查看:359
本文介绍了使用drawRect设置UIView backgroundColor:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个自定义的UIView类的背景颜色。该类还在 drawRect:方法中执行石英绘图。

I am trying to set a custom UIView class's background color. The class also does quartz drawing in the drawRect:method.

由于背景颜色更改不会发生,直到下一次重绘视图,我更改了UIView的 backgroundColor 属性之前调用 setNeedsDisplay 。我已经设置一个 UIActivityIndi​​catorView 动画时,视图是重新绘制。

Since background color change does not take place until the next redraw of the view, I change the UIView's backgroundColor property before calling setNeedsDisplay. I have set a UIActivityIndicatorView to animate while the view is redrawing.

self.backgroundColor = theColor; 
[indicator startAnimating];
[self performSelectorInBackground:@selector(setNeedsDisplay) withObject:nil];

指示器在 setNeedsDisplay theColor 将在每次我需要调用这个时改变。

The indicator is stopped at the end of setNeedsDisplay. theColor will change every time I need to call this.

假设我有一个耗时的 setNeedsDisplay 过程。我想设置背景和保持指标动画。目前,改变 backgroundColor 调用 setNeedsDisplay 但是甚至不改变backgroundColor直到 performSelectorInBackground 方法运行!因此,我的应用程序挂起,没有指示器是动画。
如何处理这个排序问题?谢谢。

Let's say I have a time consuming setNeedsDisplay process. I would like to set the background and keep the indicator animation. Currently, changing backgroundColor calls setNeedsDisplay but doesn't even change the backgroundColor until the performSelectorInBackground method runs! Therefore my app hangs and no indicator is ever animated. How do I deal with this ordering problem? Thanks.

编辑:我的意思是我的 drawrect:可能很耗时。

I meant that my drawrect: may be time consuming.

推荐答案


假设我有一个耗时的setNeedsDisplay进程

Let's say I have a time consuming setNeedsDisplay process

我们不要。您没有任何业务优先于 setNeedsDisplay 。我不是很清楚你最终要完成什么,但这整个问题似乎是一个误会如何绘制。当你调用 setNeedsDisplay (这,正如你所告诉的,你必须在主线程中),那是;你站出来的方式,当重绘时刻到来时,你的视图的 drawRect:被调用。这是绘图。

Let's not. You have no business overriding setNeedsDisplay. I am not at all clear on what you're ultimately trying to accomplish but this entire question seems to be a misunderstanding of how to draw. When you call setNeedsDisplay (which, as you've been told, you must do in the main thread), that's that; you stand out of the way, and when the redraw moment comes, your view's drawRect: is called. That's drawing.

如果问题只是活动指示器永远不会进行,这是因为你永远不会给它一个机会。它也不会开始直到重画的时刻。但是你在重绘时刻之前停止活动指示器!很明显,你永远不会看到它。

If the problem is simply that the activity indicator never gets going, that's because you never give it a chance. It too is not going to start going until the redraw moment. But you are stopping the activity indicator before the redraw moment even comes! So obviously you'll never see it go.

在下一个事情之前,显式地启动一个活动指示符的方法是走出主线程之后下一个重画时刻。这被称为延迟性能。示例:

The way to start an activity indicator visibly before the next thing you do is to step out to the main thread after the next redraw moment. This is called "delayed performance". Example:

self.backgroundColor = theColor; 
[indicator startAnimating];
double delayInSeconds = 0.1;
dispatch_time_t popTime =
    dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    // do something further, e.g. call setNeedsDisplay
};

您可以通过调用 dispatch_after

然而,我必须印象深刻的是,如果绘画的行为需要很长时间,你需要一个活动指标来覆盖它,你的绘画行为必须非常快,你可能想看看这个主题的WWDC 2012视频,它提供了如何有效绘制的优秀提示。

I must impress upon you, however, that if the mere act of drawing takes so long that you need an activity indicator to cover it, you're drawing wrong. Your act of drawing must be very very fast. You might want to watch the WWDC 2012 video on this very topic; it gives excellent tips on how to draw efficiently.

这篇关于使用drawRect设置UIView backgroundColor:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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