drawRect和addSubview:自定义绘图会影响哪些视图? [英] drawRect and addSubview: custom drawing affects which views?

查看:204
本文介绍了drawRect和addSubview:自定义绘图会影响哪些视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有 UIView 的自定义子类实现 drawRect ,并且控制器方法使用 addSubview 在此自定义视图中创建视图层次结构, drawRect 如何与这些子视图交互?它是否从头开始重新创建整个子类的视图层次结构并删除任何现有的子视图?或者它是否忽略子视图并仅重绘特定视图/子视图?

If I have a custom subclass of UIView that implements drawRect and controller methods use addSubview to create a view hierarchy in this custom view, how does drawRect interact with these subviews? Does it recreate the entire subclass's view hierarchy from scratch and remove any existing subviews? Or does it ignore subviews and only redraw a particular view/subview?

以编程方式添加和删除 drawRect

Would it be acceptable to programmatically add and remove subviews within drawRect?

推荐答案

drawRect 意味着用于在视图中绘制内容。

drawRect is meant to be only for drawing your content in the view.

是否绘制整个视图或部分视图:这取决于您的实现。如果你想进行任何优化,最好检查你的视图何时调用 drawRect 并相应地调整代码(也许你只想更新视图的一部分,也许你不想画所有的时间,等等)。这取决于您的需求

Whether it draws the entire view or part of it: It depends on your implementation. If you want to do any optimization is a good idea to check when your view calls drawRect and adjust the code accordingly (maybe you want to update only one part of the view, maybe you don't want to draw all the times, etc). It depends on your needs

我不认为在 drawRect 中添加/删除子视图是个好主意,因为这个方法会在几种情况下被调用,我敢说这不是你想要的:)

I don't think is a good idea to add/remove subviews within drawRect because this method will be called in several situations and I dare to say that is NOT what you want :)

相反,你可以尝试这样的事情:

Instead, you could try something like this:

[myView addSubview:aSubview];
[myView setNeedsDisplay];
//or calculate the needed display rect by yourself and then
[myView setNeedsDisplayInRect:aRect];

这篇关于drawRect和addSubview:自定义绘图会影响哪些视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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