【问题标题】:ios - Rectangles with borders overlappingios - 边框重叠的矩形
【发布时间】:2013-09-09 14:54:28
【问题描述】:

当我尝试绘制相互重叠的矩形时,我遇到了一个奇怪的问题。 见下图:

如您所见,顶线比其他线(底线和垂直线)更清晰,尤其是比分隔矩形的线更清晰。 我使用了以下代码:

for (int i = 0; i < 7; i++)
{

    (...)
    CGContextBeginPath(context);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, self.cellBorder);
    CGRect dayRect;
    if (i > 0)
        dayRect   = CGRectMake(i*cellWidth+self.marginX - 1, 0, cellWidth, cellHeight);
    else
        dayRect   = CGRectMake(i*cellWidth+self.marginX , 0, cellWidth, cellHeight);
    CGContextStrokeRect(context, dayRect);

}

有什么建议吗?

【问题讨论】:

    标签: ios cgrectmake


    【解决方案1】:

    顶线比其他线更细的原因是您有一个大于 0 的self.cellBorder 线粗,并且您将其绘制在y = 0 所在的线上。当你这样做时,你只会看到线条粗细的一半,因为另一半在绘图矩形上方。要解决此问题,您只需在 y 位置 self.cellBorder / 2 处绘制顶线。以下是代码的变化方式:

    for (int i = 0; i < 7; i++) {
        // ...
        CGContextBeginPath(context);
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextSetLineWidth(context, self.cellBorder);
        CGRect dayRect;
        if (i > 0)
            dayRect = CGRectMake(i*cellWidth+self.marginX - 1, self.cellBorder / 2, cellWidth, cellHeight);
        else
            dayRect = CGRectMake(i*cellWidth+self.marginX , self.cellBorder / 2, cellWidth, cellHeight);
        CGContextStrokeRect(context, dayRect);
    }
    

    【讨论】:

    • 就像数学一样简单!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2018-11-02
    • 2020-09-09
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多