关于drawRect:方法apple官方文档的翻译

原文

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.

译文:

该方法的默认实现并不会做任何事情。子类使用诸如Core Graphics和UIKit技术绘制其控件的内容应该重写该方法,并且把实现的代码写在该方法中。如果你控件的内容是用其他方式设置的,那么你就不需要重写该方法。例如,如果你的控件仅仅只是展示背景颜色,则不需要重写该方法或者你的控件内容是直接使用layer对象设置的,也不需要调用该方法。

当调用该方法时,UIKit框架已经为你的控件配置好合适的绘制环境,你可以轻松的调用任何绘制方法和函数来渲染你的控件内容。特别地,UIKit会创建并配置一个图形上下文,然后调整该上下文的形变,以使该图形上下文的原点与你控件的bounds的原点相匹配。你可以通过调用UIGraphicsGetCurrentContext函数获得图形上下文的引用,但是不要对该图形上下文建立强引用,因为多次调用drawRect:方法期间,图形上下文会改变。

如果你直接创建UIView的子类,该方法的实现不需要调用super。但是,如果你创建的是不同的view类,你需要在实现代码的某个时候调用super。

当一个view第一次显示或当一个事件发生,该事件导致view的可视部分无效,永远不要手动调用该方法。为了使控件的某个部分失效,并且因此导致某个部分重绘,应该调用setNeedsDisplay或者setNeedsDisplayInRect:而不是drawRect:方法。




转载自:http://www.jianshu.com/p/960eb666eaef

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值