daspianist
daspianist

Reputation: 5495

Drawing a circular transparent view inside a UIView

I am trying to draw a transparent circle UIView. Based on the answer from this question, I have decided to make a UIView subclass called PartialTransparentView to create an animating transparent circular view.

I am having issues in the drawRect method in this subclass's implementation to get the draw transparent view to actually appear as circular rather than the default rectangular. Based on the following code and the suggestions from this answer, I should be setting the context first and then filling in the colors later. However, even after trying several other code permutations it still isn't working. Any advice would be very helpful!

- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();
    [backgroundColor setFill];
    UIRectFill(rect);

    for (NSValue *holeRectValue in rectsArray) {

        CGRect holeRect = [holeRectValue CGRectValue];
        CGContextAddEllipseInRect(context, holeRect);
        CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );

        [[UIColor clearColor] setFill];
        UIRectFill(holeRectIntersection);
    }

}

Upvotes: 0

Views: 647

Answers (1)

sha
sha

Reputation: 17860

You need to change blend mode before erasing your circles:

CGContextSetBlendMode(context, kCGBlendModeClear)

Upvotes: 1

Related Questions