简体   繁体   English

UIView setNeedsDisplay调用CALayer的setNeedsDisplay吗?

[英]UIView setNeedsDisplay calling the CALayer's setNeedsDisplay?

It seems that calling [self setNeedsDisplay] on a UIView does not result in a [self.layer setNeedsDisplay] . 似乎在UIView上调用[self setNeedsDisplay]不会导致[self.layer setNeedsDisplay] Is it alright if I override setNeedsDisplay as follows: 如果我如下重写setNeedsDisplay ,可以吗?

- (void)setNeedsDisplay {
  [super setNeedsDisplay];
  [self.layer setNeedsDisplay];
}

Will this get me into trouble? 这会给我带来麻烦吗? Perhaps there is a better way? 也许有更好的方法? Thank You 谢谢

I posted this as a comment to another answer, but this may be helpful to other people. 我将其发布为对另一个答案的评论,但这可能对其他人有帮助。

A UIView only calls setNeedsDisplay on its underlying CALayer when drawRect: is implemented (even if the method is empty and does nothing). 实现drawRect:时,UIView仅在其基础CALayer上调用setNeedsDisplay (即使该方法为空drawRect:执行任何操作)。 This tells UIView's setNeedsDisplay that its layer also has some drawing to be done. 这告诉UIView的setNeedsDisplay ,它的图层也需要完成一些绘制。

Another way to get the layer's setNeedsDisplay to be called automatically is to set the layer's flag needsDisplayOnBoundsChange to true, which will trigger a setNeedsDisplay whenever the view's bounds change (which automatically changes the layer's bounds). 自动调用图层的setNeedsDisplay另一种方法是将图层的标志needsDisplayOnBoundsChange为true,只要视图的边界发生变化(这会自动更改图层的边界),就会触发setNeedsDisplay But to answer your questions, what you're doing is fine if you didn't override drawRect: , otherwise it's redundant. 但是要回答您的问题,如果您不重写drawRect:那么您正在做的事情就很好了,否则就多余了。

I don't see why it would get you into trouble. 我不明白为什么会惹上你麻烦。 setNeedsDisplay / setNeedsLayout just flag that the view/layer needs to be displayed / laid out. setNeedsDisplay / setNeedsLayout只是标记视图/图层需要显示/布局。 It'll do the drawing next time around the run loop. 下次将在运行循环中进行绘制。

I'm fairly surprised though that calling it on the view doesn't cause it to happen on the layer, because after all a UIView and its associated CALayer are quite tightly integrated. 尽管在视图上调用它并不会导致它在图层上发生,但我感到非常惊讶,因为毕竟UIView及其关联的CALayer紧密集成在一起。

So overall, yeh if you're finding it's not working for you by just calling setNeedsDisplay on the view then go for your solution. 所以总的来说,是的,如果您发现它不适合您,只需在视图上调用setNeedsDisplay ,然后寻求解决方案。 I don't see how it can harm anything. 我看不出它有什么危害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM