简体   繁体   中英

UIView setNeedsDisplay calling the CALayer's setNeedsDisplay?

It seems that calling [self setNeedsDisplay] on a UIView does not result in a [self.layer setNeedsDisplay] . Is it alright if I override setNeedsDisplay as follows:

- (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). This tells UIView's setNeedsDisplay that its layer also has some drawing to be done.

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). But to answer your questions, what you're doing is fine if you didn't override drawRect: , otherwise it's redundant.

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. 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.

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. I don't see how it can harm anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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