本文介绍了CoreGraphics系列似乎改变了尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这一行围绕着我的形状:





问题是,它显然从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);

任何想法?

解决方案

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



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

您想要做的是在两个维度中按.5插入 rect ,调整图形上下文以使其来源在 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);
}

结果如下:




I have this line around my shape:

The problem is, it obviously goes from 1px thick, to 2px. Here's the code I'm using:

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);

Any ideas?

解决方案

The reason your top, bottom, and right edges appear thinner than your diagonals is because you are chopping off half of the lines drawn along those edges. As Costique mentioned, Core Graphics draws a stroke so that it is centered on the path. That means half of the stroke lies on one side of the path, and half of the stroke lies on the other side. Since your path runs exactly along the top, right, and bottom edges of your view, half of the stroke falls outside the bounds of your view and isn't drawn.

Costique is also correct that you should move your paths in by .5 points. (Technically you should move by half the stroke width.) However, you're not specifying your coordinates uniformly based on the rect variable, so moving all your paths is difficult.

What you want to do is inset rect by .5 in both dimensions, adjust your graphics context so that its origin is at rect.origin, and use only rect.size to get your coordinates - not self.frame.size. Here's my test:

- (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);
}

Here's the result:

这篇关于CoreGraphics系列似乎改变了尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:46