MegaManX
MegaManX

Reputation: 8918

iPhone how to clip half of the ellipse

I have drawn ellipse:

CGContextFillEllipseInRect(contextRef, CGRectMake(50, 50, 50, 128));

But i only need a half of ellipse, is there a way to clip the other half?

Upvotes: 1

Views: 1058

Answers (1)

Felix Lamouroux
Felix Lamouroux

Reputation: 7484

Before calling the drawing method you can clip the context to a portion of the ellipse:

CGContextSaveGState(contextRef);
BOOL onlyDrawTopHalf = YES;
CGFloat halfMultiplier = onlyDrawTopHalf ? -1.0 : 1.0;
CGRect ellipse = CGRectMake(50, 50, 50, 128);
CGRect clipRect = CGRectOffset(ellipse, 0, halfMultiplier * ellipse.size.height / 2);
CGContextClipToRect(contextRef, clipRect);
CGContextFillEllipseInRect(contextRef, ellipse);
// restore the context: removes the clipping
CGContextRestoreGState(contextRef);

Upvotes: 5

Related Questions