CGContextSetRGBStrokeColor 不会绘制边框

发布于 2024-12-07 09:25:34 字数 2560 浏览 8 评论 0 原文

我正在尝试创建一个饼图。每个饼图应该有不同的颜色并有边框。所以我制作了自己的类 PieChart.m:

#import "PieChart.h"


@implementation PieChart
@synthesize startDeg, endDeg, isSelected, colorNumber;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    int centerX = 120;
    int centerY = 160;
    int radius  = 94;

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 255.0/255.0, 255.0/255.0, 255.0/255.0, 1.0);
    CGContextSetLineWidth(ctx, 2.0);

    CGContextSetRGBFillColor(ctx, [self getColorFor:1]/255.0, [self getColorFor:2]/255.0, [self getColorFor:3]/255.0, 1.0);

    CGContextMoveToPoint(ctx, centerX, centerY);
    CGContextAddArc(ctx, centerX, centerY, radius, (startDeg)*M_PI/180.0, (endDeg)*M_PI/180.0, 0); 

    CGContextClosePath(ctx);
    CGContextFillPath(ctx);
}

- (void)dealloc
{
    [super dealloc];
}

-(float)getColorFor:(int)rgb {
    if (colorNumber == 1) {
        switch (rgb) {
            case 1:
                return 232.0;
                break;
            case 2:
                return 96.0;
                break;
            case 3:
                return 104.0;
                break;

            default:
                return 255.0;
                break;
        }
    }
    if (colorNumber == 2) {
        switch (rgb) {
            case 1:
                return 248.0;
                break;
            case 2:
                return 198.0;
                break;
            case 3:
                return 6.0;
                break;

            default:
                return 255.0;
                break;
        }
    }

    return 255.0;
}

@end

问题是边框永远不会绘制。或者,如果我设法绘制边框,填充就不会在那里!有什么建议如何实现这一点?

以下是我在视图控制器中使用该类的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    PieChart *pie1 = [[PieChart alloc] initWithFrame:CGRectMake(10, 10, 300, 400)];
    pie1.startDeg = 0;
    pie1.endDeg = 127;
    pie1.colorNumber = 1;
    pie1.backgroundColor = [UIColor clearColor];

    PieChart *pie2 = [[PieChart alloc] initWithFrame:CGRectMake(10, 10, 300, 400)];
    pie2.startDeg = 127;
    pie2.endDeg = 360;
    pie2.colorNumber = 2;
    pie2.backgroundColor = [UIColor clearColor];

    [self.view addSubview:pie1];
    [self.view addSubview:pie2];
}

I am trying to create a pie chart. Each pie should be with different color and have it's border. So I made my own class PieChart.m:

#import "PieChart.h"


@implementation PieChart
@synthesize startDeg, endDeg, isSelected, colorNumber;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    int centerX = 120;
    int centerY = 160;
    int radius  = 94;

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 255.0/255.0, 255.0/255.0, 255.0/255.0, 1.0);
    CGContextSetLineWidth(ctx, 2.0);

    CGContextSetRGBFillColor(ctx, [self getColorFor:1]/255.0, [self getColorFor:2]/255.0, [self getColorFor:3]/255.0, 1.0);

    CGContextMoveToPoint(ctx, centerX, centerY);
    CGContextAddArc(ctx, centerX, centerY, radius, (startDeg)*M_PI/180.0, (endDeg)*M_PI/180.0, 0); 

    CGContextClosePath(ctx);
    CGContextFillPath(ctx);
}

- (void)dealloc
{
    [super dealloc];
}

-(float)getColorFor:(int)rgb {
    if (colorNumber == 1) {
        switch (rgb) {
            case 1:
                return 232.0;
                break;
            case 2:
                return 96.0;
                break;
            case 3:
                return 104.0;
                break;

            default:
                return 255.0;
                break;
        }
    }
    if (colorNumber == 2) {
        switch (rgb) {
            case 1:
                return 248.0;
                break;
            case 2:
                return 198.0;
                break;
            case 3:
                return 6.0;
                break;

            default:
                return 255.0;
                break;
        }
    }

    return 255.0;
}

@end

The problem is that the borders would never draw. Or if I manage to draw the border the fill wouldn't be there! Any suggestions how this should be achieved?

Here is how I use the class in my viewcontroller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    PieChart *pie1 = [[PieChart alloc] initWithFrame:CGRectMake(10, 10, 300, 400)];
    pie1.startDeg = 0;
    pie1.endDeg = 127;
    pie1.colorNumber = 1;
    pie1.backgroundColor = [UIColor clearColor];

    PieChart *pie2 = [[PieChart alloc] initWithFrame:CGRectMake(10, 10, 300, 400)];
    pie2.startDeg = 127;
    pie2.endDeg = 360;
    pie2.colorNumber = 2;
    pie2.backgroundColor = [UIColor clearColor];

    [self.view addSubview:pie1];
    [self.view addSubview:pie2];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落成白 2024-12-14 09:25:34

您的代码的问题是您只填充了路径,而不是抚摸它 - 所以现在自然地绘制了边框。您需要将 CGContextFillPath 调用替换为

CGContextDrawPath (ctx, kCGPathFillStroke);

The problem with your code is that you only fill your path, not stroking it - so naturally border is now drawn. You need to replace CGContextFillPath call with

CGContextDrawPath (ctx, kCGPathFillStroke);
爱你是孤单的心事 2024-12-14 09:25:34

抚摸或填充路径会清除当前路径。这就是为什么一个有效而另一个无效(无论您先尝试哪个)。

您可以通过在抚摸之前重新添加路径或使用 CGContextDrawPath 来解决此问题

Stroking or filling the path clears the current path. That's why one works but not the other (whichever one you try first).

You can fix this by re-adding your path before stroking, or using CGContextDrawPath

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文