从drawRect中绘制UIView的边界之外

发布于 2024-10-05 02:20:41 字数 584 浏览 0 评论 0原文

我的问题与此非常相似当clipToBounds=NO时不绘制外部边界没有得到明确的答复。

基本上我有一个 UIView,我想从它的中心到屏幕的边缘画一条线。使用 [self ConvertPoint:(CGPoint){0,0} fromView:[self superview]]; 计算这些点的位置很容易(它找到相对于我的视图的超级视图的原点。但是当我从我的视图的drawRect绘制一条线:它在我的视图边界处被剪切。

有没有办法在我的视图边界之外绘制?我尝试过更改clipsToBounds属性,但它似乎没有任何效果

。无法从超级视图中绘制我的线条,因为我需要使用多个视图来执行此操作,并且有些视图会在其他视图的前面...从超级视图的drawRect中找出图层似乎是一个坏主意,

同样,我不认为这是 一个坏主意。我可以调整视图的边界大小以包含整个屏幕,因为我的视图需要动态调整大小......边界必须很大(> 20,000 平方点)才能工作。

My question is very similar to this one Not drawing outside bounds when clipToBounds=NO which received no clear answer.

Basically I have a UIView, and I want to draw a line from the center of it, to the edge of the screen. Calculating where these points are is easy, using [self convertPoint:(CGPoint){0,0} fromView:[self superview]]; (which finds the origin with respect to my view's superview. But when I draw a line from my view's drawRect: it gets clipped at my view's bounds.

Is there a way to draw outside of my view's bounds? I've tried changing the clipsToBounds property, but it doesn't seem to have any effect.

I can't draw my lines from the superview because I need to do this with multiple views and some will be in front of others... figuring out the layer from the superview's drawRect seems like a bad idea.

Similarly, I don't think I can just resize my view's bounds to include the entire screen, because my views need to be dynamically re-sizable... the bounds would have to be HUGE (>20,000 points square) for this to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-10-12 02:20:41

我不建议在视图的边界之外进行绘制。您的视图需要自动调整大小以包含您的绘图,或者您需要具有透明的重叠视图。或者两者兼而有之。我想不出这两种情况都不能涵盖的情况,但我可能缺乏想象力。 =)

目前可能发生的情况是,当超级视图被重绘时,它告诉超级视图它需要重绘,从而导致擦除您正在外面进行的绘图。已经有一段时间了,如果我错了,任何更有知识的人都可以(应该!)在这里纠正我。

我不知道“Quartz Debug”(来自标准苹果开发人员工具安装,/Developer/Applications/Performance Tools/Quartz Debug)是否在模拟器中工作,但值得一试。它有一种模式,可以显示重绘发生的时间和频率,并带有边框和可选的刷新延迟。

您可以按照您的要求进行操作,但是每次超出子视图的边界时都需要强制重绘子视图,这意味着您的超级视图需要在其绘制函数内手动绘制其子视图。本质上,您将抛弃苹果的绘图范例,而只是使您的子视图充当主视图的绘图扩展。

此外,如果您的范围如此动态,您可能需要考虑以屏幕或超级视图的百分比而不是点来绘制,那么编码可能更有意义。

I wouldn't recommend ever drawing outside of a view's bounds. Either your view needs to resize automatically to include your drawing or you need to have transparent overlapping views. Or both. I can't think of a situation that either of these cases wouldn't cover, but I may lack imagination. =)

Likely what is happening currently is that when the super view gets redrawn it tells the super view that it needs redrawn, resulting in erasing the drawing you are doing outside. It's been a while, anyone more knowledgeable can (should!) correct me here if I'm wrong.

I don't know if "Quartz Debug" (from the standard apple developer tools install, /Developer/Applications/Performance Tools/Quartz Debug) works in the simulator, but it's worth a try. It has a mode that will show you when and how often redrawing takes place, with a border and optional delay on the refreshes.

You can do what you are asking, but you need to force redraw your sub-views every time you go outside the sub-view's bounds, meaning that your super-view needs to manually draw it's children inside of it's draw function. Essentially you would be throwing out apple's drawing paradigm and simply causing your sub-views to act like a drawing extension of your main view anyway.

Additionally, if your ranges are so dynamic you may want to consider drawing in percentages of the screen or super-view rather than in points, it may make more sense to code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文