UIBezierPath 厚度不均匀

Uneven thickness in UIBezierPath

我正在试验 UIBezierPath。我想制作网格视图。

NSInteger yAxisIncremental = 0;
NSInteger xAxisIncremental = 0;

UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 1;

for (int x = 0; x <5; x++) {
    [path moveToPoint:CGPointMake(xAxisIncremental, 0)];
    [path addLineToPoint:CGPointMake(xAxisIncremental, self.frame.size.height)];
    xAxisIncremental = xAxisIncremental + kWidthOfSquare;
}

for (int x = 0; x <=5; x++) {
    [path moveToPoint:CGPointMake(0, yAxisIncremental)];
    [path addLineToPoint:CGPointMake(self.frame.size.width, yAxisIncremental)];
    yAxisIncremental = yAxisIncremental + kHeightOfSquare;
}

[[UIColor blackColor] setStroke];
[path stroke];

请注意第一行很细,相应的行也有点 thick.Have 我在代码中做错了什么,或者这是预期的行为吗?

您绘制的第一条线从 0 开始。这意味着该线的一半宽度可能会在屏幕外或您的视野之外。您可以通过从像素 0.5 而不是 0 开始来解决此问题。

此外,如果您选择 1.0 的线宽,这意味着在高 dpi 设备上,线将比屏幕上的 1 个实际像素粗。要绘制一条宽度为 1px 的线,您可以使用 lineWidth 为 0,如 UIBezierPath 文档中所述:

A width of 0 is interpreted as the thinnest line that can be rendered on a particular device.

这里是一些示例代码,用于生成具有笔划安全区域的矩形点。

objective-c

  UIBezierPath *aPath = [UIBezierPath bezierPath];
    CGFloat safeArea  = 1.5;
    CGFloat minX = rect.origin.x+safeArea;
    CGFloat minY = rect.origin.y+safeArea;
    CGFloat maxX = rect.origin.x+rect.size.width-safeArea;
    CGFloat maxY = rect.origin.y+rect.size.height-safeArea;
    //draw your rectangle with stroke safe area*2
...

.