0

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

}
Community
  • 1
  • 1
daspianist
  • 5,336
  • 8
  • 50
  • 94

1 Answers1

1

You need to change blend mode before erasing your circles:

CGContextSetBlendMode(context, kCGBlendModeClear)
sha
  • 17,824
  • 5
  • 63
  • 98
  • Thanks for the suggestion. I've added `CGContextSetBlendMode(context, kCGBlendModeClear);` right before `CGContextAddEllipseInRect(context, holeRect);` as suggested, but it does not appear to work. As a precaution, I also tried places like right after getting the context, but it isn't also working. Could you elaborate a bit on the answer? Thank you! – daspianist Nov 28 '14 at 03:50