我正在尝试在 UIView 上绘制一个字符数组。问题是视图不会自动刷新,因此不会显示字符。当我们在这里和那里触摸屏幕时,它就会显示出来(有时只是字符的一部分)。当我们重绘一组新的字符时(当数组更新时),旧的也不会被清除。虽然重绘之前绘制的更新数组不会从 UIView 中清除,但它只是在同一个视图上覆盖并显示两者。
这是我的代码:
在 MyView 类中。
-(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 类
// 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)];
}
}
}