38

I currently have two UIViews: one of a red background and the other blue. The blue view is a subview of the red view. What I would like to do is be able to "cut" out rectangles on the blue view so that the red view can be visible. How do you go about doing this?

Ajumal
  • 1,048
  • 11
  • 33
Dave
  • 435
  • 1
  • 6
  • 5
  • For a drop-in solution, see my answer to this question: http://stackoverflow.com/questions/14141081/uiview-drawrect-draw-the-inverted-pixels-make-a-hole-a-window-negative-space/31255084#31255084 – clozach Jul 06 '15 at 20:45
  • I ended up posting my solution for this problem here: http://stackoverflow.com/a/32941652/1670083 – James Laurenstin Oct 05 '15 at 05:12

5 Answers5

76

You have to override the top view's drawRect method. So, for example, you might create a HoleyView class that derives from UIView (you can do that by adding a new file to your project, selecting Objective-C subclass, and setting "Subclass of" to UIView). In HoleyView, drawRect would look something like this:

- (void)drawRect:(CGRect)rect {
    // Start by filling the area with the blue color
    [[UIColor blueColor] setFill];
    UIRectFill( rect );

    // Assume that there's an ivar somewhere called holeRect of type CGRect
    // We could just fill holeRect, but it's more efficient to only fill the
    // area we're being asked to draw.
    CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );

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

If you're using Interface builder, make sure to change the holey view's class to HoleyView. You can do that by selecting in the view in Interface Builder and selecting the "Identity" pane in the inspector (its the one on the far right the the "i" icon).

You also have to set the top view to be non-opaque either with the following code snippet, or by un-checking the Opaque checkbox in the view's properties in Interface Builder (you'll find it in the View section of the view's attributes) and set its background color's opacity to 0% (background color is set in the same section).

topView.opaque = NO;
topView.backgroundColor = [UIColor clearColor];

If you want to do circles, you have to use Core Graphics (aka Quartz 2D). You'll probably want to read the programming guide, which is available here.

To draw an ellipse instead of the rectangle, your drawRect would look something like this:

- (void)drawRect:(CGRect)rect {
    // Get the current graphics context
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor( context, [UIColor blueColor].CGColor );
    CGContextFillRect( context, rect );

    if( CGRectIntersectsRect( holeRect, rect ) )
    {
        CGContextSetFillColorWithColor( context, [UIColor clearColor].CGColor );
        CGContextFillEllipseInRect( context, holeRect );
    }
}
KlimczakM
  • 12,576
  • 11
  • 64
  • 83
Jacques
  • 6,050
  • 1
  • 31
  • 24
  • 8
    `UIGraphicsFillRect` with `clearColor` set as the fill color won't erase the rect it will paint empty color on top of the existing contents. `CGContextClearRect` is the only function that can "erase" part of a `CGContext` (not that is should be used in the general case--it's much better to mask the earlier drawing commands) – rpetrich Sep 27 '10 at 01:53
  • I agree that masking could be a better way to go, but the code above will work (after fixing my boneheaded error where I used the wrong name for UIFillRect). I just ran it on a device to double-check. I did leave out that you need a transparent background for the view as well, so either do topView.background = [UIColor clearColor] or set the view's background color's opacity to 0. – Jacques Sep 27 '10 at 02:10
  • Awesome! Thanks guys, that worked like a charm. This works great for rectangles, but how would it work for different shapes (i.e. circle)? – Dave Sep 28 '10 at 06:24
  • Dave, I added an example for drawing a circle. If you want to do arbitrary shapes, you need to create a path. Take a look at the Quartz 2D programming guide and the CGContext reference manual for details. Also, if you like an answer, please click the checkmark beside it to mark it as accepted :-). – Jacques Sep 29 '10 at 03:06
  • @Dave My app requirement is something like this, when I move my finger on blue view, I need to draw a circle on the blue view through that circle I should be able to see redview. How can I do this? – Dee Jan 13 '12 at 11:39
  • This answer is so full of awesome. Thanks a lot. +1! – Kalle Jan 17 '12 at 12:28
  • @Jacques The `UIRectFill()` works fine, but `CGContextFillRect` with a `clearColor` seems draw nothing depending on my test. – studyro Dec 07 '12 at 06:22
  • So close... The hole draws once ( my clear view is draggable), but then calls: : CGContextSetCompositeOperation: invalid context 0x0 – Morkrom Sep 04 '13 at 23:45
  • I am having a terrible time trying to draw holes with shadows... tips or pointers would be appreciated... – livingtech Oct 07 '13 at 19:30
  • My eclipse is always in black colour, even when I use CGContextSetBlendMode(context, kCGBlendModeClear), opaque = NO and backgroundColor = [UIColor clearColor]. Does anyone know why? – Josip B. Oct 15 '14 at 15:15
  • This answer did not work for me. I had to add CGContextSetBlendMode(context, kCGBlendModeClear) as suggested by another answer. – avance Jun 26 '15 at 16:57
  • @studyro you need to set `opaque` to false, this step is needed. – Dima Deplov Apr 09 '16 at 17:42
35

There is truth in all the other answers, but it is quite possible to draw with clear colour, or so to say erase the existing colours within any path, even with the -[UIBezierPath fill] or similar convenience methods. All you have to do is to set the context blend mode to an appropriate value depending on the effect you are trying to achieve, like so:

CGContextSetBlendMode(context, kCGBlendModeClear);
[[UIColor clearColor] set];
[myArbitraryPolygonPath fill];

There are around two dozen different options you could choose from, take a look around the CGContext reference.

rage
  • 1,447
  • 16
  • 16
  • You don't even need to set a clear color. The clear blend mode is going to set the premultiplied pixel value to 0 regardless of the source color. – CIFilter Dec 14 '16 at 20:56
  • Could you expand on how to use this for my issue: http://stackoverflow.com/questions/42066123/mask-all-views-below-a-uiview-except-background – damjandd Feb 06 '17 at 11:20
  • 1
    This is great. To use in Swift, call UIBezierPath.fill(with blendMode: CGBlendMode, alpha: CGFloat) – Mathieson Jun 10 '17 at 06:06
17

to draw an ellipse, instead of a rect, just set the blendMode to clear:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor( context, [UIColor blackColor].CGColor );
    CGContextFillRect( context, rect );

    CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );

    CGContextSetFillColorWithColor( context, [UIColor clearColor].CGColor );
    CGContextSetBlendMode(context, kCGBlendModeClear);

    CGContextFillEllipseInRect( context, holeRect );
}
valvoline
  • 7,737
  • 3
  • 47
  • 52
1

These may be the dumb ways but I would not do it the way you describe, but make it look the way you want it to look.

(Jacques' answer just popped up - looks good to me)

Approach 1: In the view controller to build a list of rectangles that tile around the exposed "hole". As your holes increase in number the number of tiling rects will also increase.

Approach 2: Reverse your thinking. The blue view should be at the back with sections of the red view placed on top of it. You still see a red view with all but the "holes" masked by the blue view, but what you are really doing is copying regions from the view you want to expose and putting them on top of the mask as you make each hole. If you have some effect simulating depth you could add that as required with each hole.

Neither requires subclassing or drawRect:.

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
0

Some of the answers are quite old.

2023, Latest techniques include...

 class CompImage: UIIImageView {

     override func common() {
         super.common()
         layer.compositingFilter = "multiplyBlendMode"
         
         print("Filters available to you ...\n",
          CIFilter.filterNames(inCategory: kCICategoryCompositeOperation))
     }
 }

and

guard let ctx = UIGraphicsGetCurrentContext() else { return }
ctx.saveGState()
ctx.setStrokeColor(
..
ctx.setBlendMode(.multiply) // review the choices available
..
Fattie
  • 27,874
  • 70
  • 431
  • 719