当我们重新绘制字符串时,UIView不会清除(刷新)

时间:2013-05-16 11:36:09

标签: ios uiview refresh drawrect setneedsdisplayinrect

我正在尝试在UIView上绘制一个char数组。问题是视图没有自动刷新,因此不显示字符。当我们在这里和那里触摸屏幕时,它会被显示(有时只显示一部分字符)。当我们重新绘制新的字符集时(当数组更新时),旧的字符集也不会被清除。虽然重绘先前绘制的更新数组并未从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)];
         }
   }
}

2 个答案:

答案 0 :(得分:0)

问题是setNeedsDisplayInRect并不能保证立即重新绘制UIView,如果你查看那个说明的那个苹果文档

  

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

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

后尝试调用此方法

答案 1 :(得分:0)

@amar:请注意drawRect的文档:明确说明“你不应该直接自己调用这个方法。”

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