buildsucceeded
buildsucceeded

Reputation: 4243

Custom drawing invisible until quit

In my subclass of UIView, custom drawing. The original drawing works fine, i.e. when triggered from viewDidLoad or else from a swipe or shake gesture, which clears the screen and redraws.

The problem comes when I try to draw on top of what's already there-- it behaves erratically, usually invisible and offset from where I've placed it, but then it appears if I press the home button. On home-button press, as the view disappears I get a glimpse of what I've tried to draw. If I relaunch the app all the drawing is there. What gives?

Here's how I'm set up:

- (void)drawRect:(CGRect)rect; {
    cgRect = [[UIScreen mainScreen] bounds];
    cgSize = cgRect.size;
        context = UIGraphicsGetCurrentContext();

    for (int i=0; i<(cgSize.width / spacing); i++) {
        for (int j=0; j<(cgSize.height / spacing); j++) {
            [self drawObjectAtPosX:i*spacing andY:j*spacing withId:i*(cgSize.height / spacing) + j];
         }
     }

There are invisible buttons over certain objects. When they are pressed, the object is to be redrawn in a different color:

-(void)buttonPressed:theButton {
int which = [theButton tag];
if ([[objectArray objectAtIndex:which] shouldBeRedrawn]) {
        [self redrawObjectforID:which];
    }
}

-(void)redrawObjectforID:(int)which {
        myObject *chosenOne = [objectArray objectAtIndex:which];
        CGFloat m_x = chosenOne.x;
        CGFloat m_y = chosenOne.y;
        context = UIGraphicsGetCurrentContext();
        CGContextSetRGBStrokeColor(context, 205/255.0,173/255.0,0.0,1.0); // LINE color
        CGContextSetRGBFillColor(context, 205/255.0,197/255.0,0.0,1.0); // FILL color
        CGContextTranslateCTM(context, m_x, m_y);
        CGContextRotateCTM(context, chosenOne.rotation);
        for (int i=0; i<4; i++) {
            [self drawObjectSegmentatX:m_x andY:m_y forID:which inContext:context]; // custom drawing stuff
            CGContextRotateCTM(context, radians(90));
        } 
        CGContextRotateCTM(context, -chosenOne.rotation);
        CGContextTranslateCTM(context, -m_x, -m_y);
}

Upvotes: 0

Views: 137

Answers (1)

buildsucceeded
buildsucceeded

Reputation: 4243

Answering my own, but here's what I learned today:

All custom drawing needs to be in DrawRect. The glitchy behavior I was seeing was the result of DrawRect being called by the view disappearing or being reinstated. Moved, and it works.

Location of the custom redraw is still off, but that's another question.

Upvotes: 1

Related Questions