kumar
kumar

Reputation: 51

UIView does not clear (Refresh) when we reDraw String

I am trying to draw a char array on a UIView. The problem is that the view is not refreshing automatically and hence the chars are not displayed. When we touch the screen here and there then it gets displayed (sometime only a portion of the char). When we are redrawing new set of chars (when array is updated) the old one doesn't gets cleared as well. While redrawing the updated array previously drawn doesn't get cleared from UIView, it just over writes on same view and displays both.

Here is my code:

In MyView Class.

-(void)drawRect:(CGRect)rect
{
  context=UIGraphicsGetCurrentContext();
}

-(void)initializeWithChar:(char)characterToDisplay andAttribute:(Byte)attribute
{
    displayChar = characterToDisplay;
    displayCharAttribute = attribute;
}

-(void)setNeedsDisplayInRect:(CGRect)rect{

    NSString *charString;
    UIGraphicsPushContext(context);
    charString = [NSString stringWithFormat:@"%c",displayChar];

    // while background color is black.
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor); 

    [charString drawInRect:CGContextConvertRectToUserSpace(context, rect) 
                  withFont:[UIFont fontWithName:@"Courier New" size:10] 
             lineBreakMode:NSLineBreakByWordWrapping 
                 alignment:NSTextAlignmentLeft];

    UIGraphicsPopContext();
}

-(void)clearScreenInRect:(CGRect) rect{
    [self.viewForBaselineLayout setClearsContextBeforeDrawing:YES];
    [self.viewForBaselineLayout setOpaque :NO];
    CGContextClearRect (context,rect);

    // I TRIED THIS AS WELL -> but dosen't work..
    //CGContextSetFillColorWithColor(contxt, [UIColor blackColor].CGColor);
    //CGContextFillRect(contxt, rect) ;

}

writeDrawView class

// CODE BELOW is DRAWING FRESH ARRAY ON VIEW

//this method draw a sign in form on view (content are placed in SignInFormArray)
-(void)writeToDrawViewFrom_SignInFormArray{
 for (int rownumber = 0; rownumber < 10; rownumber++) {
    for (int columnNumber = 0; columnNumber < 30; columnNumber++) {
              [myView initializeWithChar:SignInFormArray[rownumber][columnNumber] andAttribute:Attribute[rownumber][columnNumber]];
              [myView setNeedsDisplayInRect:CGRectMake(columnNumber*12 , rownumber*30, 12, 30)];
         }
   }
}

-(void)clearButtonClicked{
         for (int rownumber = 0; rownumber < 10; rownumber++) {
            for (int columnNumber = 0; columnNumber < 30; columnNumber++) {
                [myView clearScreenInRect:CGRectMake(columnNumber*12 , rownumber *30  , 12, 30)];
                }
        }
}


// CODE BELOW is UPDATING VIEW FROM UPDATED ARRAY

//this method draw a home page menu form on view (content are placed in HomePageMenuArray)
-(void)writeToDrawViewFrom_HomePageMenuArray{
 for (int rownumber = 0; rownumber < 10; rownumber++) {
    for (int columnNumber = 0; columnNumber < 30; columnNumber++) {
              [myView initializeWithChar:HomePageMenuArray[rownumber][columnNumber] andAttribute:Attribute[rownumber][columnNumber]];
              [myView setNeedsDisplayInRect:CGRectMake(columnNumber*12 , rownumber*30, 12, 30)];
         }
   }
}

Upvotes: 0

Views: 669

Answers (2)

Amit
Amit

Reputation: 25

@amar:Do note that the documentation for drawRect: explicitly says "You should never call this method directly yourself."

I also have same Problem: While redrawing the updated array previously drawn doesn't get cleared from UIView, it just over writes on same view and displays both. And When any key on iOS keyboard is pressed I need to draw a character at that particular place on UIView.

Upvotes: 0

danypata
danypata

Reputation: 10175

The problem is that the setNeedsDisplayInRect doesn't guarantee an immediate redrawn of the UIView, if you check the apple doc which says that

This method adds the specified rectangle into the view’s current list of invalid rectangles and returns immediately.

So you have to force a redraw after calling this method (from my experience the bringSubviewToFront method can cause a redraw of the views). So you should try calling this method after calling the setNeedsDisplayInRect.

Upvotes: 0

Related Questions