原文
Discussion
The default implementation of this method does nothing. Subclasses that use 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.
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 the UIGraphicsGetCurrentContext function, but do not establish a strong reference to the graphics context because it can change between calls to the drawRect: method.
Similarly, if you draw using OpenGL ES and the GLKView class, GLKit configures the underlying OpenGL ES context appropriately for your view before calling this method (or the glkView:drawInRect: method of your GLKView delegate), so you can simply issue whatever OpenGL ES commands you need to render your content. For more information about how to draw using OpenGL ES, see OpenGL ES Programming Guide for iOS.
You should limit any drawing to the rectangle specified in the rect parameter. In addition, if the opaque property of your view is set to YES, your drawRect: method must totally fill the specified rectangle with opaque content.
If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.
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.
譯文:
該方法的默認(rèn)實(shí)現(xiàn)并不會(huì)做任何事情。子類使用諸如Core Graphics和UIKit技術(shù)繪制其控件的內(nèi)容應(yīng)該重寫該方法,并且把實(shí)現(xiàn)的代碼寫在該方法中。如果你控件的內(nèi)容是用其他方式設(shè)置的,那么你就不需要重寫該方法。例如,如果你的控件僅僅只是展示背景顏色,則不需要重寫該方法或者你的控件內(nèi)容是直接使用layer對(duì)象設(shè)置的,也不需要調(diào)用該方法。
當(dāng)調(diào)用該方法時(shí),UIKit框架已經(jīng)為你的控件配置好合適的繪制環(huán)境,你可以輕松的調(diào)用任何繪制方法和函數(shù)來渲染你的控件內(nèi)容。特別地,UIKit會(huì)創(chuàng)建并配置一個(gè)圖形上下文,然后調(diào)整該上下文的形變,以使該圖形上下文的原點(diǎn)與你控件的bounds的原點(diǎn)相匹配。你可以通過調(diào)用UIGraphicsGetCurrentContext函數(shù)獲得圖形上下文的引用,但是不要對(duì)該圖形上下文建立強(qiáng)引用,因?yàn)槎啻握{(diào)用drawRect:方法期間,圖形上下文會(huì)改變。
如果你直接創(chuàng)建UIView的子類,該方法的實(shí)現(xiàn)不需要調(diào)用super。但是,如果你創(chuàng)建的是不同的view類,你需要在實(shí)現(xiàn)代碼的某個(gè)時(shí)候調(diào)用super。
當(dāng)一個(gè)view第一次顯示或當(dāng)一個(gè)事件發(fā)生,該事件導(dǎo)致view的可視部分無效,永遠(yuǎn)不要手動(dòng)調(diào)用該方法。為了使控件的某個(gè)部分失效,并且因此導(dǎo)致某個(gè)部分重繪,應(yīng)該調(diào)用setNeedsDisplay或者setNeedsDisplayInRect:而不是drawRect:方法。