iOS:使用UIView的'drawRect:'与其图层的委托'drawLayer:inContext:' [英] iOS: Using UIView's 'drawRect:' vs. its layer's delegate 'drawLayer:inContext:'

查看:380
本文介绍了iOS:使用UIView的'drawRect:'与其图层的委托'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 绘图委托方法的原因。您也应该永远不要查看另一个图层的绘图委托。这将导致各种各样的怪异。

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.

如果你正在实现 drawLayer:inContext:因为你需要访问 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:'与其图层的委托'drawLayer:inContext:'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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