我什么时候需要在 iOS 中调用 setNeedsDisplay?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10818319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:34:01  来源:igfitidea点击:

When do I need to call setNeedsDisplay in iOS?

iosobjective-cswiftuiviewsetneedsdisplay

提问by Nosrettap

When creating an iOS app, I'm confused as to when exactly I need to call setNeedsDisplay? I know that it has something to do with updating/redrawing the UI; however, do I need to call this every time I change any of my views?

当创建一个iOS应用程序,我很困惑,什么时候我需要调用setNeedsDisplay?我知道它与更新/重绘用户界面有关;但是,每次我改变我的观点时都需要调用它吗?

For example, do I need to call it:

例如,我是否需要调用它:

  • After programatically changing the text in a text field
  • When changing the background of a view?
  • When I make changes in viewDidLoad?
  • How about in viewDidAppear?
  • 以编程方式更改文本字段中的文本后
  • 何时更改视图的背景?
  • 当我在 viewDidLoad 中进行更改时?
  • 在 viewDidAppear 中怎么样?

Could someone give me some general guidelines regarding when to use this method?

有人能给我一些关于何时使用这种方法的一般指南吗?

回答by Amogh Talpallikar

You should only be calling setNeedsDisplay if you override drawRect in a subclass of UIView which is basically a custom view drawing something on the screen, like lines, images, or shapes like a rectangle.

只有在 UIView 的子类中覆盖 drawRect 时,才应该调用 setNeedsDisplay,UIView 基本上是在屏幕上绘制某些内容的自定义视图,例如线条、图像或矩形等形状。

So you should call setNeedsDisplay when you make changes to few variables on which this drawing depends and for view to represent that change , you need to call this method which internally will give a call to drawRect and redraw the components.

因此,当您更改此绘图所依赖的几个变量时,您应该调用 setNeedsDisplay 并且为了表示该更改的视图,您需要调用此方法,该方法将在内部调用 drawRect 并重绘组件。

When you add an imageView or a UIButton as a subview or make changes to any subview, you need not call this method.

当您添加一个 imageView 或一个 UIButton 作为子视图或对任何子视图进行更改时,您不需要调用此方法。

Example:

例子:

You have a view that shows a moving circle, either you touch and move it, or may be timer based animation. Now for this, you will need a custom view that draws a circle at given center and with given radius. These are kept as instance variables which are modified to move the circle by changing its center or make it bigger by increasing radius of it.

您有一个显示移动圆圈的视图,您可以触摸并移动它,也可以是基于计时器的动画。现在为此,您将需要一个自定义视图,该视图在给定的中心和给定的半径处绘制一个圆。这些被保留为实例变量,它们被修改为通过改变圆心来移动圆或通过增加半径来使其变大。

Now in this case either you will modify these variables(centre or radius) in a loop and timer Or may be by your fingers in touchesEnded and touchesMoved methods. To reflect the change in this property you need to redraw this view for which you will call setNeedsDisplay.

现在在这种情况下,您将在循环和计时器中修改这些变量(中心或半径),或者可以通过您的手指在 touchesEnded 和 touchesMoved 方法中进行修改。要反映此属性的更改,您需要重绘此视图,您将为其调用 setNeedsDisplay。

回答by Mike Weller

You only really need to call -setNeedsDisplayon UIView subclasses that draw their contents with -drawRect:.

你真的只需要调用-setNeedsDisplayUIView 子类来绘制它们的内容-drawRect:

For labels and other standard controls, changing the text will automatically cause the label to redraw so you don't need to do this yourself.

对于标签和其他标准控件,更改文本将自动导致标签重绘,因此您无需自己执行此操作。

回答by Wei Xiang

setNeedsDisplay:should be called when you want to refresh your view explicitly. It just sets an internal flag, and the iOS UI system will call drawRect:at an appropriate time later.

setNeedsDisplay:当您要显式刷新视图时应调用。它只是设置一个内部标志,iOS UI 系统会drawRect:在稍后的适当时间调用。

It sounds like it should be always called when you updating any property which may change the presentation. But it's not. Almost all the standard UI controls already handled that. I believe whenever you modify the properties of standard UI components (views), setNeedsDisplay:would be triggered internally, and the affected region will be redrawn. (In all the situations you listed)

当您更新任何可能更改演示文稿的属性时,听起来应该始终调用它。但事实并非如此。几乎所有的标准 UI 控件都已经处理过了。我相信每当您修改标准 UI 组件(视图)的属性时,setNeedsDisplay:都会在内部触发,并且会重绘受影响的区域。(在您列出的所有情况下)

However, if you create your own view, implement its own drawRect:, and want to update that when something has been changed, you must call setNeedsDisplay:explicitly.

但是,如果您创建自己的视图,实现自己的drawRect:,并希望在某些内容发生更改时更新它,则必须setNeedsDisplay:显式调用。

回答by Proton

I think @Amogh Talpallikar make it clear. And I just wanna discuss one thing more.

我认为@Amogh Talpallikar 说清楚了。我只想再讨论一件事。

In the fact that, you should avoid override drawRectunless you really need it because it can cause bad performance. You can refer this https://yalantis.com/blog/mastering-uikit-performance/

事实上,drawRect除非你真的需要它,否则你应该避免覆盖,因为它会导致糟糕的性能。你可以参考这个https://yalantis.com/blog/mastering-uikit-performance/

If you only wanna change frame, position of buttons, labels, ... you can call setNeedLayoutor layoutIfNeeded

如果你只想改变框架,按钮的位置,标签,......你可以打电话setNeedLayoutlayoutIfNeeded

回答by SuryaKantSharma

You will call setNeedDisplay when you are changing the property on which your view custom drawing depends. It will explicitly call drawRect: method forcefully.

你会打电话给setNeedDisplay当你正在改变,你的看法自定义绘图所依赖的属性。它将显式地强制调用 drawRect: 方法。