Objective c 在不使用drawRect的情况下在视图中绘制多个矩形:

Objective c 在不使用drawRect的情况下在视图中绘制多个矩形:,objective-c,cocoa-touch,uiview,drawrect,Objective C,Cocoa Touch,Uiview,Drawrect,我有一个代码,它在一个UIView中生成多个矩形: CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(ctx, 0.5); CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor); //newView.xPoints is always equal in count to the other two arrays bel

我有一个代码,它在一个
UIView
中生成多个矩形:

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(ctx, 0.5);

CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);

//newView.xPoints is always equal in count to the other two arrays below.    
for (int i = 0; i < [newView.xPoints count]; i++) 
{
    CGFloat tx = [[newView.xPoints objectAtIndex:i]floatValue];
    CGFloat ty = [[newView.yPoints objectAtIndex:i]floatValue];
    CGFloat charWidth = [[newView.charArray objectAtIndex:i]floatValue];

    CGRect rect = CGRectMake(tx, (theContentView.bounds.size.height - ty), charWidth, -10.5);
    CGContextAddRect(ctx, rect);
    CGContextStrokePath(ctx);
}
CGContextRef ctx=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx,0.5);
CGContextSetStrokeColorWithColor(ctx,[UIColor blueColor].CGColor);
//newView.xPoints的计数始终等于下面的其他两个数组。
对于(int i=0;i<[newView.xPoints count];i++)
{
CGFloat tx=[[newView.xPoints objectAtIndex:i]floatValue];
CGFloat ty=[[newView.yPoints objectAtIndex:i]floatValue];
CGFloat charWidth=[[newView.charArray objectAtIndex:i]floatValue];
CGRect rect=CGRectMake(tx,(contentview.bounds.size.height-ty),charWidth-10.5);
CGContextAddRect(ctx,rect);
CGContextStrokePath(ctx);
}

我在
drawRect:
中试用过,效果非常好。但是,我计划将
drawRect:
用于其他目的。那么,有谁能给我一些提示或提示,告诉我如何不用
drawRect:

你不能。
drawRect的内部:
是当前上下文进入屏幕的唯一时间。您必须使“其他用途”与此矩形绘图代码共存


但是,您可以将此代码分解为另一个方法,只要它仅从内部调用
drawRect:

,您就不能这样做。
drawRect的内部:
是当前上下文进入屏幕的唯一时间。您必须使“其他用途”与此矩形绘图代码共存


但是,您可以将此代码分解为另一个方法,只要它仅从内部调用
drawRect:

,您就不能这样做。只有
UIGraphicsGetCurrentContext()
drawRect:
中对视图有效。您可以在drawRect之外绘制一个图层,但如果您想实际查看该图层,则必须在
drawRect:
中将该图层绘制到视图中

你不能。只有
UIGraphicsGetCurrentContext()
drawRect:
中对视图有效。您可以在drawRect之外绘制一个图层,但如果您想实际查看该图层,则必须在
drawRect:
中将该图层绘制到视图中

你知道,你可以在drawRect中做很多事情。你可以把代码包装在一个函数中,然后从drawRect调用它。你知道,你可以在drawRect中做很多事情。您可以将该代码包装在函数中,并从draw rect调用它。