我什么时候需要在iOS中调用setNeedsDisplay? [英] When do I need to call setNeedsDisplay in iOS?

查看:138
本文介绍了我什么时候需要在iOS中调用setNeedsDisplay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建iOS应用时,我很困惑何时需要调用 setNeedsDisplay ?我知道它与更新/重绘UI有关;但是,每次更改我的任何观点时,是否需要调用它?

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?

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

For example, do I need to call it:


  • 以编程方式更改文本字段中的文本后

  • 更改视图背景时?

  • 当我在viewDidLoad中进行更改时?

  • viewDidAppear怎么样?

  • 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?

有人可以给我一些关于何时使用的一般指导这个方法?

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

推荐答案

如果你在UIView的子类中覆盖drawRect,你应该只调用setNeedsDisplay,它基本上是一个自定义视图绘图屏幕上的某些内容,如线条,图像或矩形等形状。

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.

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

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.

当您将imageView或UIButton添加为子视图或对任何子视图进行更改,您无需调用此方法。

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

示例:

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

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.

现在在这种情况下你要么在循环和计时器中修改这些变量(中心或半径),要么可以用touchesEnded和touchesMoved方法。
要反映此属性的更改,您需要重新绘制此视图,您将调用setNeedsDisplay。

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.

这篇关于我什么时候需要在iOS中调用setNeedsDisplay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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