【发布时间】:2012-07-09 05:29:13
【问题描述】:
我有这段代码可以在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中做很多事情,你知道的。您可以将该代码包装在一个函数中并从 draw rect 中调用它。
标签: objective-c cocoa-touch uiview drawrect