我有一個UIView
並在其中我已經繪製了使用核心圖形通過覆蓋drawRect
。這個視圖還包含一個子視圖,它也繪製了一條線。然而,儘管這兩種觀點都使用幾乎相同的代碼(用於測試至少),他們繪製的線條不會出現相同的:iPhone核心圖形爲子視圖較粗的虛線
正如你所看到的 - 在虛線頂部明顯比底部厚,我不知道爲什麼。以下是兩個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
方法中添加了該問題。
第二個視圖:
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);
爲什麼要在第一個視圖的'drawRect:'中添加子視圖? – jer 2010-11-19 22:34:57
@jer:這一定很重要嗎?我可以從另一個方法如'initWithFrame'添加子視圖,但是線寬仍然存在差異。除非我不打算將子視圖添加到本身「繪製」並因此出現問題(?)的視圖上,但我沒有在任何地方讀過......但是。 – JoeR 2010-11-19 22:41:24
@JoeR,是的,它可能很重要 - 每次您的視圖自己繪製時都會添加子視圖,因此您可以通過放置多個相同的子視圖來結束視覺效果 - 這可能會導致子視圖線變得更粗,因爲它們被繪製多次 – Vladimir 2010-11-19 22:44:12