我有这段代码,它可以在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);
}
我在drawRect:
试过了,效果很好。但是,我计划将drawRect:
用于其他目的。那么任何人都可以给我一些提示或提示,说明如何在不使用drawRect:
的情况下做到这一点?
你不能。drawRect:
内部是当前上下文进入屏幕的唯一时间。您必须使您的"其他用途"与此矩形绘图代码共存。
但是,您可以将此代码分解为另一个方法,只要它仅从内部调用 drawRect:
。
你不能。 UIGraphicsGetCurrentContext()
对您的视图有效的唯一时间是在 drawRect:
内。 您可以绘制到 drawRect 之外的图层,但如果您想实际看到该图层,您仍然必须在drawRect:
中将该图层绘制到您的视图中。