iOS:使用UIView的“drawRect:”与其层的delagate“drawLayer:inContext:” [英] iOS: Using UIView's 'drawRect:' vs. its layer's delagate 'drawLayer:inContext:'

查看:112
本文介绍了iOS:使用UIView的“drawRect:”与其层的delagate“drawLayer:inContext:”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类是 UIView 的子类。我可以通过实现 drawRect 方法或通过实现 drawLayer:inContext:来绘制内容是一个委托方法 CALayer

I have a class which is a subclass of UIView. I am able to draw stuff inside the view either by implementing the drawRect method, or by implementing drawLayer:inContext: which is a delegate method of CALayer.

我有两个问题:


  1. 如何决定使用哪种方法?是否有一个用例?

  2. 如果我实现 drawLayer:inContext:,它被调用(和 drawRect 不是,至少只要放置一个断点就可以告诉),即使我没有将视图分配为 CALayer 委托使用:

  1. How to decide which approach to use? Is there a use case for each one?
  2. If I implement drawLayer:inContext:, it is called (and drawRect isn't, at least as far as putting a breakpoint can tell), even if I don't assign my view as the CALayer delegate by using:

[[self layer] setDelegate:self];

如果我的实例没有定义为图层的委托,那么委托方法如何被调用?如果 drawLayer:inContext:被调用,什么机制阻止调用 drawRect

how come the delegate method is called if my instance is not defined to be the layer's delegate? and what mechanism prevents drawRect from being called if drawLayer:inContext: is called?


推荐答案


如何决定使用哪种方法?有没有一个用例?

How to decide which approach to use? Is there a use case for each one?

始终使用 drawRect:并且不要使用 UIView 作为任何 CALayer 的绘图委托。

Always use drawRect:, and never use a UIView as the drawing delegate for any CALayer.


如果我的实例没有被定义为图层的委托,那么委托方法如何被调用?如果调用了 drawLayer:inContext:,什么机制阻止drawRect被调用?

how come the delegate method is called if my instance is not defined to be the layer's delegate? and what mechanism prevents drawRect from being called if drawLayer:inContext: is called?

每个 UIView 实例是用于其支持的图纸委托 CALayer 。这就是为什么 [[self layer] setDelegate:self]; 似乎什么都不做。这是多余的 drawRect:方法实际上是视图层的绘图委托方法。在内部, UIView 实现 drawLayer:inContext:它在哪里做一些自己的东西,然后调用 drawRect:。您可以在调试器中看到它:

Every UIView instance is the drawing delegate for its backing CALayer. That's why [[self layer] setDelegate:self]; seemed to do nothing. It's redundant. The drawRect: method is effectively the drawing delegate method for the view's layer. Internally, UIView implements drawLayer:inContext: where it does some of its own stuff and then calls drawRect:. You can see it in the debugger:

这就是为什么 drawRect:在实现 drawLayer时没有被调用:inContext :。这也是为什么你不应该在自定义的 UIView 子类中实现任何 CALayer 绘制委托方法。你也应该永远不要让任何人看到另一层的图纸委托。如果您正在实现 drawLayer:inContext:,因为您需要访问这些文件,那么这将导致各种糟糕的事情。

This is why drawRect: was never called when you implemented drawLayer:inContext:. It's also why you should never implement any of the CALayer drawing delegate methods in a custom UIView subclass. You should also never make any view the drawing delegate for another layer. That will cause all sorts of wackiness.

CGContextRef ,您可以通过调用 UIGraphicsGetCurrentContext()从 drawRect: / code>。

If you are implementing drawLayer:inContext: because you need to access the CGContextRef, you can get that from inside of your drawRect: by calling UIGraphicsGetCurrentContext().

这篇关于iOS:使用UIView的“drawRect:”与其层的delagate“drawLayer:inContext:”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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