我有一个UIView
在其中我通过覆盖使用 Core Graphics 画了一条线drawRect
。该视图还包含一个子视图,该子视图也绘制一条线。然而,虽然两个视图都使用几乎相同的代码(至少出于测试目的),但在它们上绘制的线看起来并不相同:
正如你所看到的 - 顶部的虚线明显比底部的虚线粗,我不知道为什么。下面是两人使用的代码UIViews
在他们的drawRect
方法。如果您知道为什么会发生这种情况,那么我将非常感谢您的帮助和建议!
第一个视图:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGFloat dashes[] = {1,1};
CGContextSetLineDash(context, 0.0, dashes, 2);
CGContextSetLineWidth(context, 0.6);
CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGContextStrokePath(context);
SubUIView *view = [[SubUIView alloc] initWithFrame:rect];
[self addSubview:view];
[view release];
该视图肯定只被绘制一次。我很欣赏drawRect
可能不是添加子视图的最佳位置,但即使将其添加到主视图中,问题仍然存在initWithFrame
method.
第二个视图:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGFloat dashes[] = {1,1};
CGContextSetLineDash(context, 0.0, dashes, 2);
CGContextSetLineWidth(context, 0.6);
CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMidY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMidY(rect));
CGContextStrokePath(context);