Question

I'm currently having a problem with custom drawing with core graphics. When I try to draw a kind of rectangle with a rounded border, i get an extra line that i don't want. I've read the documentation and it says that when you draw an arc it draw a line to the origin of the arc. But i don't succeed in finding a solution that would avoid this line. Here is a pic : enter image description here

So i want to avoid this grey line at the top, under my arc

And here is my code :

//Tracer
    CGContextBeginPath(context);
    CGContextSetStrokeColorWithColor(context, wormBorderColor);
    CGContextSetFillColorWithColor(context, wormFillColor);

    //Tracer la ligne de gauche
    CGContextMoveToPoint(context, leftX, startingY);
    CGContextAddLineToPoint(context, leftX, startingY-wormHeight);
    //CGContextClosePath(context);

    //tracer l'arc supérieur
    CGContextMoveToPoint(context, rightX, startingY-wormHeight);
    CGContextAddArc(context, centerX, startingY-wormHeight, echelleSize/2, 0,M_PI , 1);
    CGContextClosePath(context);

    CGContextAddLineToPoint(context, rightX, startingY);



    //CGContextAddRect(context, CGRectMake(leftX, startingY, rightX-leftX, wormHeight));
    //CGContextClosePath(context);


    CGContextStrokePath(context);

Please excuse my poor english.

Was it helpful?

Solution

The horizontal line at the bottom of the arc is caused by the CGContextClosePath. To make this same image without the horizontal line change the code:

//tracer l'arc supérieur
CGContextMoveToPoint(context, rightX, startingY-wormHeight);
CGContextAddArc(context, centerX, startingY-wormHeight, echelleSize/2, 0,M_PI , 1);
CGContextClosePath(context);

CGContextAddLineToPoint(context, rightX, startingY);

to:

//tracer l'arc supérieur
CGContextMoveToPoint(context, rightX, startingY-wormHeight);
CGContextAddArc(context, centerX, startingY-wormHeight, echelleSize/2, 0,M_PI , 1);


CGContextMoveToPoint(context, rightX, startingY-wormHeight);
CGContextAddLineToPoint(context, rightX, startingY);

Edit:

Modified the code to draw the path in a way that it can be filled, and added a fill at the end.

//Tracer
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, wormBorderColor);
CGContextSetFillColorWithColor(context, wormFillColor);

//Déplacez à l'origine
CGContextMoveToPoint(context, leftX, startingY);

//Tracer la ligne de gauche
CGContextAddLineToPoint(context, leftX, startingY-wormHeight);

//tracer l'arc supérieur - clockwise
CGContextAddArc(context, centerX, startingY-wormHeight, echelleSize/2, 0,M_PI , 0);

//Tracer la ligne de droite
CGContextAddLineToPoint(context, rightX, startingY);

CGContextStrokePath(context);
CGContextFillPath(context);

Sorry for my poor French ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top