【发布时间】:2012-04-07 08:46:46
【问题描述】:
我正在使用 Core Graphics 在 iphone 上制作一个圆角矩形浮动对话框。当沿同心圆角矩形路径应用笔划时,笔划之间的间隙总是出现在角落中。这些相同的笔画并排放置在直线段上。
代码的相关摘录(无关代码已删除):
-(void)drawRect:(CGRect)rect {
CGRect borderRect = CGRectInset(rect, 1.0, 1.0);
UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:borderRect cornerRadius:6.0];
[...]
CGContextSetStrokeColorWithColor(context, bevelStrokeColor);
CGContextSetLineWidth(context, 2.0);
CGContextAddPath(context, borderPath.CGPath);
CGContextStrokePath(context);
[...]
CGRect inlayRect = CGRectInset(rect, inlayPathInset, inlayPathInset);
UIBezierPath *inlayPath = [UIBezierPath bezierPathWithRoundedRect:inlayRect cornerRadius:6.0];
[...]
CGContextSetStrokeColorWithColor(context, inlayStrokeColor);
CGContextSetLineWidth(context, 2.0);
CGContextAddPath(context, inlayPath.CGPath);
CGContextStrokePath(context);
[...]
}
这是一张图片:
我尝试了上面发布的代码的不同版本。我已经手动创建了路径,使用 UIBezierPath 实例方法来创建和绘制,并且我已经通过视图的图层以及其他一些想法以各种组合设置了cornerRadius。他们中的一些人改善了这些问题,但并没有得到消除。
如果我只使用边框,我不会介意,但我也将实施一些同心圆角矩形,这个差距将是一个问题。
编辑:更正了转录时出现的代码拼写错误。
【问题讨论】:
-
这里只是猜测一下,因为我没有看到您的代码有问题,也无法从图片中看出,但是当实例化
inlayPath时变量cornerRadius的值是多少?莫非是6.0之类的边框路径?因为您需要记住,borderPath 内侧的半径小于外侧的半径。因此,您的 inlayPaths 角半径需要小于 6.0。如果它等于或大于你所看到的效果。 -
糟糕。我忘记将其编辑为 6.0。我把它从其他路径初始化中去掉了。他们都在 6 岁,但这是不对的。我现在正试图推导它背后的数学。如果我有什么想法,我会分享。
标签: iphone ios core-graphics rounded-corners quartz-2d