用于子视图的iPhone Core Graphics更粗的虚线 [英] iPhone Core Graphics thicker dashed line for subview
问题描述
我有一个 UIView
,在其中我通过覆盖 drawRect
使用Core Graphics绘制了一条线。此视图还包含一个也绘制线的子视图。但是,虽然两个视图使用几乎相同的代码(至少用于测试目的),但它们上绘制的线条看起来并不相同:
I have a UIView
and within it I've drawn a line using Core Graphics by overriding drawRect
. This view also contains one subview which also draws a line. However, whilst both views are using pretty much the same code (for testing purposes at least), the lines drawn on them do not appear the same:
如您所见 - 顶部的虚线明显比底部厚,我不明白为什么。下面是两个 UIViews
在其 drawRect
方法中使用的代码。如果你知道为什么会这样,那么我将非常感谢你的帮助和建议!
As you can see - the dashed line at the top is noticeably thicker than the bottom one and I have no idea why. Below is the code used by the two UIViews
in their drawRect
methods. If you have any idea why this is happening then I'd appreciate your help and advice!
第一视图:
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
方法。
The view is definitely only being drawn once. I appreciate drawRect
may not be the best place for adding a subview but the problem remains even with it added in the main 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);
推荐答案
如果您的抗锯齿可能是rect不会落在整数上。您可以使用 CGContextSetShouldAntialias(上下文,NO)
禁用消除锯齿功能。我认为还有一个用于制作矩形积分的函数,但我不记得它是什么。
It could be a result of anti-aliasing if your rect does not fall on integers. You can disable anti-aliasing with CGContextSetShouldAntialias( context, NO )
. I think there's also a function for making a rect integral, but I can't remember what it is.
这篇关于用于子视图的iPhone Core Graphics更粗的虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!