0

我正在尝试在 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)];
         }
   }
}
4

2 回答 2

0

@amar:请注意 drawRect: 的文档明确表示“您永远不应该自己直接调用此方法。”

我也有同样的问题:虽然重绘之前绘制的更新数组并没有从 UIView 中清除,但它只是在同一个视图上覆盖并显示两者。并且当按下 iOS 键盘上的任何键时,我需要在 UIView 上的特定位置绘制一个字符。

于 2013-05-16T14:42:49.207 回答
0

问题是setNeedsDisplayInRect不保证立即重绘 UIView,如果你检查苹果文档的话

此方法将指定的矩形添加到视图的当前无效矩形列表中并立即返回。

因此,您必须在调用此方法后强制重绘(根据我的经验,该bringSubviewToFront方法可能会导致视图重绘)。所以你应该在调用setNeedsDisplayInRect.

于 2013-05-16T12:42:16.993 回答