2

I need to draw incrementally in a subclassed UIView, but the view is cleared every time I call [self setNeedsDisplay].

I'm doing this:

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        self.clearsContextBeforeDrawing = FALSE;
    }
    return self;
}

Am I missing something? How do I stop UIView from clearing?

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
Henrietta
  • 53
  • 2
  • 5

2 Answers2

3

If you call setNeedsDisplay it will call drawRect with the full bounds.

If you are drawing incrementally (portions of the view), you need to call:

[self setNeedsDisplayInRect:rect];

Of course that means your drawRect needs to be able to handle just drawing a portion of the view.

More info here: Drawing incrementally in a UIView (iPhone)

Also, you're setting it to FALSE and it's typically NO in objective-c. One resolves to 0 and the other to (BOOL)0 at compile time. A bit odd but not causing issues:

Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • oh rats, that is quite disappointing. I need to animate very fine shading incrementally over the entire image. setNeedsDisplayInRect will not work at all. – Henrietta Nov 29 '11 at 15:52
  • ultimately I switched to a UIImageView and shaded my graphics iteratively on a timer… ok then. – Henrietta Jan 10 '12 at 02:35
3

Sorry.

drawRect: is not guaranteed use the saved contents of any previous drawing of a regular UIView. The framebuffer is likely behind an opaque path to the GPU, and can be thought of as essentially write only. So you always have to be able to recreate the entire view if you implement a drawRect for a regular UIView.

But you can draw into another context including previous graphics contents if you create your own bitmap drawing context, and draw into that context instead of a UIView. You can then convert that bitmap context into an image and display that image as the content of the CALayer of a UIView.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • @hotpaw2, Can I do my drawing directly on the previously drawn content of the CALayer (without using intermediate bitmap context)? – Benji Mizrahi Oct 06 '17 at 20:30