用于子视图的iPhone Core Graphics更粗的虚线

Joe*_*oeR 8 iphone core-graphics uiview

我有一个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];
Run Code Online (Sandbox Code Playgroud)

该视图绝对只被绘制一次.我很欣赏drawRect可能不是添加子视图的最佳位置,但问题仍然存在于主initWithFrame方法中.

第二观点:

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);
Run Code Online (Sandbox Code Playgroud)

Bri*_*ian 6

如果你的矩形没有落在整数上,它可能是抗锯齿的结果.您可以使用禁用抗锯齿功能CGContextSetShouldAntialias( context, NO ).我认为还有一个使矩形积分的功能,但我不记得它是什么.

  • 这是CGRectIntegral() (3认同)