UIView中的drawRect方法

来源:互联网 发布:dsp广告系统架构php 编辑:程序博客网 时间:2024/05/19 07:08

Draws the receiver’s image within the passed-in rectangle.

用传进来的矩形绘制图形。

- (void)drawRect:(CGRect)rect

Parameters

rect

The portion of the view’s bounds that needs to be updated. The first time your view is drawn, this rectangle is typically the entire visible bounds of your view. However, during subsequent drawing operations, the rectangle may specify only part of your view.

Discussion

The default implementation of this method does nothing. Subclasses that use native drawing technologies (such as Core Graphics and UIKit) to draw their view’s content should override this method and implement their drawing code there. You do not need to override this method if your view sets its content in other ways. For example, you do not need to override this method if your view just displays a background color or if your view sets its content directly using the underlying layer object. Similarly, you should not override this method if your view uses OpenGL ES to do its drawing.

这个方法默认的实现不做任何东西。使用原生画图技术绘制视图内容的子类(比如Core Graphics and UIKit)应该覆盖这个方法并且实现画图代码。如果你的视图以其他方式设置内容的时候你不应该覆盖这个方法。比如,如果你的视图只是显示背景颜色或者你的视图用基础的层对象设置它的内容时,你不需要覆盖这个方法。相似地,如果你的视图使用OpenGL ES去画图你也不需要覆盖这个方法。

By the time this method is called, UIKit has configured the drawing environment appropriately for your view and you can simply call whatever drawing methods and functions you need to render your content. Specifically, UIKit creates and configures a graphics context for drawing and adjusts the transform of that context so that its origin matches the origin of your view’s bounds rectangle. You can get a reference to the graphics context using theUIGraphicsGetCurrentContext function, but do not retain the graphics context because it can change between calls to thedrawRect: method.

这个方法被调用时,UIKit已经为你的视图配置了画图环境,并且你可以调用任何你需要的画图方法和函数去展现你的内容。特别地,UIKit为画图和调整内容的转化创建并且配置了图形上下文,这样它的初始点可以匹配你的视图边界矩形的初始点。你可以使用UIGraphicsGetCurrentContext函数去获得一个图形上下文的引用,但是不要保留这个图形上下文,因为在调用drawRect:meothod之间,他会改变。

You should limit any drawing to the rectangle specified in the rect parameter. In addition, if theopaque property of your view is set to YES, yourdrawRect: method must totally fill the specified rectangle with opaque content.

你应该把绘图限定在指定的矩形参数中。另外,如果你的视图的不透明属性设置为YES,你的drawRect:方法一定要完全用不透明的内容填充指定的矩形。

If you subclass UIView directly, your implementation of this method does not need to callsuper. However, if you are subclassing a different view class, you should callsuper at some point in your implementation.

如果你直接子类化UIView,你对这个方法的实现不需要调用super。然而,如果你是子类化一个不同的视图类,你需要调用super

This method is called when a view is first displayed or when an event occurs that invalidates a visible part of the view. You should never call this method directly yourself. To invalidate part of your view, and thus cause that portion to be redrawn, call the setNeedsDisplay or setNeedsDisplayInRect: method instead.

当一个视图第一次被展示的时候,或者当一个视图的可见区域无效时,这个方法被调用。你不需要直接调用这个方法。为了让你的视图部分无效,并且导致那个部分被重画,调用

setNeedsDisplay 或者setNeedsDisplayInRect: 方法

Availability

  • Available in iOS 2.0 and later.

See Also

  • – setNeedsDisplay
  • – setNeedsDisplayInRect:
  •   @property contentMode

Declared In

UIView.h