<bdo id='97e9a'></bdo><ul id='97e9a'></ul>

<tfoot id='97e9a'></tfoot>

<small id='97e9a'></small><noframes id='97e9a'>

    1. <legend id='97e9a'><style id='97e9a'><dir id='97e9a'><q id='97e9a'></q></dir></style></legend>

        <i id='97e9a'><tr id='97e9a'><dt id='97e9a'><q id='97e9a'><span id='97e9a'><b id='97e9a'><form id='97e9a'><ins id='97e9a'></ins><ul id='97e9a'></ul><sub id='97e9a'></sub></form><legend id='97e9a'></legend><bdo id='97e9a'><pre id='97e9a'><center id='97e9a'></center></pre></bdo></b><th id='97e9a'></th></span></q></dt></tr></i><div id='97e9a'><tfoot id='97e9a'></tfoot><dl id='97e9a'><fieldset id='97e9a'></fieldset></dl></div>

        iOS:使用 UIView 的 'drawRect:' 与其层的委托 'drawLayer:in

        时间:2023-07-28
        • <bdo id='2Imfe'></bdo><ul id='2Imfe'></ul>
          <i id='2Imfe'><tr id='2Imfe'><dt id='2Imfe'><q id='2Imfe'><span id='2Imfe'><b id='2Imfe'><form id='2Imfe'><ins id='2Imfe'></ins><ul id='2Imfe'></ul><sub id='2Imfe'></sub></form><legend id='2Imfe'></legend><bdo id='2Imfe'><pre id='2Imfe'><center id='2Imfe'></center></pre></bdo></b><th id='2Imfe'></th></span></q></dt></tr></i><div id='2Imfe'><tfoot id='2Imfe'></tfoot><dl id='2Imfe'><fieldset id='2Imfe'></fieldset></dl></div>
            <tbody id='2Imfe'></tbody>

          <small id='2Imfe'></small><noframes id='2Imfe'>

        • <legend id='2Imfe'><style id='2Imfe'><dir id='2Imfe'><q id='2Imfe'></q></dir></style></legend>

                <tfoot id='2Imfe'></tfoot>
                  本文介绍了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:

                  这就是为什么在您实现 drawLayer:inContext: 时从未调用过 drawRect:.这也是为什么您永远不应该在自定义 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.

                  如果您因为需要访问 CGContextRef 而实现 drawLayer:inContext:,则可以从 drawRect: 内部获取它通过调用 UIGraphicsGetCurrentContext().

                  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:'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:UILongPressGestureRecognizer 没有调用它的目标方法 下一篇:当我在 UITextField 中按返回键时如何隐藏键盘?

                  相关文章

                  1. <tfoot id='G5hPW'></tfoot>

                  2. <small id='G5hPW'></small><noframes id='G5hPW'>

                    <legend id='G5hPW'><style id='G5hPW'><dir id='G5hPW'><q id='G5hPW'></q></dir></style></legend>

                      <i id='G5hPW'><tr id='G5hPW'><dt id='G5hPW'><q id='G5hPW'><span id='G5hPW'><b id='G5hPW'><form id='G5hPW'><ins id='G5hPW'></ins><ul id='G5hPW'></ul><sub id='G5hPW'></sub></form><legend id='G5hPW'></legend><bdo id='G5hPW'><pre id='G5hPW'><center id='G5hPW'></center></pre></bdo></b><th id='G5hPW'></th></span></q></dt></tr></i><div id='G5hPW'><tfoot id='G5hPW'></tfoot><dl id='G5hPW'><fieldset id='G5hPW'></fieldset></dl></div>
                        <bdo id='G5hPW'></bdo><ul id='G5hPW'></ul>