【问题标题】:Drawing multiple rectangles in a view without using drawRect:不使用 drawRect 在视图中绘制多个矩形:
【发布时间】: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


【解决方案1】:

你不能。 drawRect: 内部是当前上下文唯一出现在屏幕上的时间。你必须让你的“其他目的”与这个矩形绘图代码共存。

但是,您可以将此代码分解到另一个方法中,只要它仅在 drawRect: 内部调用即可。

【讨论】:

    【解决方案2】:

    你不能。 UIGraphicsGetCurrentContext() 对您的视图有效的唯一时间是在 drawRect: 内部。您可以绘制到 drawRect 之外的图层,但如果您想真正看到它,您仍然必须将该图层绘制到 drawRect: 中的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多