CoreGraphics系列似乎改变了尺寸?

And*_*rew 2 iphone core-graphics objective-c

我的形状围绕着这条线:

在此输入图像描述

问题是,它显然从1px厚到2px.这是我正在使用的代码:

CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);

    CGContextSetFillColorWithColor(context, [BUTTON_COLOR CGColor]);
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSetLineWidth(context, 1);

    int radius = 8;


    CGContextMoveToPoint(context, 0, self.frame.size.height / 2);

    CGContextAddLineToPoint(context, POINT_WIDTH, self.frame.size.height);

    CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, 
                            rect.origin.y + rect.size.height);

    CGContextAddArc(context, rect.origin.x + rect.size.width - radius, 
                    rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);

    CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);

    CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, 
                    radius, 0.0f, -M_PI / 2, 1);

    CGContextAddLineToPoint(context, POINT_WIDTH, 0);

    CGContextAddLineToPoint(context, 0, self.frame.size.height / 2);

    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

rob*_*off 7

您的顶部,底部和右侧边缘看起来比对角线更薄的原因是因为您正在切断沿着这些边缘绘制的一半线条.正如Costique所提到的,Core Graphics绘制了一个笔划,使其路径为中心.这意味着一半的行程位于路径的一侧,而一半的行程位于另一侧.由于您的路径完全沿着视图的顶部,右侧和底部边缘运行,因此一半的笔划超出了视图的边界而未绘制.

Costique也是正确的,你应该将你的路径移动0.5个点.(从技术上讲,你应该移动笔划宽度的一半.)但是,你没有根据rect变量统一指定坐标,因此移动所有路径很困难.

您想要做的是rect在两个维度上插入.5,调整图形上下文以使其原点处于rect.origin,并且仅rect.size用于获取坐标 - 而不是self.frame.size.这是我的测试:

- (void)drawRect:(CGRect)dirtyRect
{
    CGContextRef gc = UIGraphicsGetCurrentContext();
    CGContextSaveGState(gc); {

        CGContextSetFillColorWithColor(gc, [UIColor colorWithWhite:.9 alpha:1].CGColor);
        CGContextSetStrokeColorWithColor(gc, [[UIColor blackColor] CGColor]);
        CGContextSetLineWidth(gc, 1);

        static const CGFloat Radius = 8;
        static const CGFloat PointWidth = 20;

        CGRect rect = CGRectInset(self.bounds, .5, .5);
        CGContextTranslateCTM(gc, rect.origin.x, rect.origin.x);
        rect.origin = CGPointZero;

        CGContextMoveToPoint(gc, 0, rect.size.height / 2);

        CGContextAddLineToPoint(gc, PointWidth, rect.size.height);

        CGContextAddLineToPoint(gc, rect.origin.x + rect.size.width - Radius, 
                                rect.origin.y + rect.size.height);

        CGContextAddArc(gc, rect.origin.x + rect.size.width - Radius, 
                        rect.origin.y + rect.size.height - Radius, Radius, M_PI / 2, 0.0f, 1);

        CGContextAddLineToPoint(gc, rect.origin.x + rect.size.width, rect.origin.y + Radius);

        CGContextAddArc(gc, rect.origin.x + rect.size.width - Radius, rect.origin.y + Radius, 
                        Radius, 0.0f, -M_PI / 2, 1);

        CGContextAddLineToPoint(gc, PointWidth, 0);

        CGContextAddLineToPoint(gc, 0, rect.size.height / 2);

        CGContextClosePath(gc);
        CGContextDrawPath(gc, kCGPathFillStroke);

    } CGContextRestoreGState(gc);
}
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述