ios 更改尺寸时如何强制重绘(drawRect)子视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1441760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Do I Force Redrawing (drawRect) of a SubView When it Changes Dimensions?
提问by dugla
I have a UIView that contains a row of subclasses of UIView
. The subclass has overridden drawRect
: and has set contentMode = UIViewContentModeRedraw
.
我有一个 UIView,其中包含一行UIView
. 子类已覆盖drawRect
: 并已设置 contentMode = UIViewContentModeRedraw
.
As the user squashes and stretches the parent container the child views
squash and stretch. As the shape change occurs I would like to have drawRect
called repeatedly to change the contents of the child views
. So far I have been unsuccessful. What is the correct way to do this?
当用户child views
挤压和拉伸父容器时,挤压和拉伸。随着形状发生变化,我想drawRect
反复调用以更改child views
. 到目前为止,我一直没有成功。这样做的正确方法是什么?
Cheers,
干杯,
Doug
道格
回答by Adam Wright
In Cocoa, you rarely (if ever) call drawRect - you just mark the region or view as needing update, and it will be scheduled for redrawing. This allows redraws for multiple invalidations to be collapsed together.
在 Cocoa 中,您很少(如果有的话)调用 drawRect - 您只需将区域或视图标记为需要更新,并且将安排重绘。这允许将多个失效的重绘折叠在一起。
For Cocoa Touch, you want either [view setNeedsDisplay], or setNeedsDisplayinRect:(CGRect)
对于 Cocoa Touch,您需要[view setNeedsDisplay]或setNeedsDisplayinRect:(CGRect)
回答by Pascal
You need to call [view setNeedsLayout], this should cause the subviews to receive a drawRect: call if the layout changes. I haven't tested this, however.
您需要调用 [view setNeedsLayout],这应该会导致子视图在布局更改时收到 drawRect: 调用。不过,我还没有测试过。
回答by Stan
This solution is a little crazy, but it makes logical sense, and has a great visual result. I'm working with a NSLayoutManager
and a couple NSTextContainer
s, laying out text in two columns. Because I'm using multiple text containers (and I'm not sure I want multiple UITextView
), I'm overriding the draw(rect: CGRect)
method of MyDrawingView: UIView
.
这个解决方案有点疯狂,但它合乎逻辑,并且具有很好的视觉效果。我正在处理一个NSLayoutManager
和几个NSTextContainer
s,在两列中布置文本。因为我使用的多个文本容器(我不知道我要多UITextView
),我重写draw(rect: CGRect)
的方法MyDrawingView: UIView
。
The problem is that using self.contentMode = .redraw
still results in some minor stretching on rotation--significant enough to be a deal-breaker.
问题是使用self.contentMode = .redraw
仍然会导致轮换时出现一些轻微的拉伸 - 足以破坏交易。
I noticed that setting contentMode = .left
, or even contentMode = .scaleAspectFit
was close to what I wanted, in terms of pixel-perfect rotation. But obviously, I wasn't getting the draw call I needed.
我注意到设置contentMode = .left
,甚至contentMode = .scaleAspectFit
在像素完美旋转方面接近我想要的。但很明显,我没有得到我需要的绘图调用。
So--I overrode layoutSubviews()
, switched the contentMode
to .scaleAspectFit
, called super.layoutSubviews()
, and then switched it back. :P
所以 - 我覆盖layoutSubviews()
,切换contentMode
到.scaleAspectFit
,调用super.layoutSubviews()
,然后将其切换回来。:P
override func layoutSubviews() {
self.contentMode = .scaleAspectFit
super.layoutSubviews()
self.contentMode = .redraw
}
It is...perfect.
这十分完美。