Create a UIView UIView like this:
Header (.h) file:
#import <UIKit/UIKit.h> @interface TranparentView : UIView - (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor*)color transparentRects:(NSArray*)rects circle:(BOOL)circle;
Implementation (.m) file:
#import "TranparentView.h" @implementation TranparentView { NSArray *rectsArray; UIColor *backgroundColor; BOOL isCircle; } - (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor*)color transparentRects:(NSArray*)rects circle:(BOOL)circle { backgroundColor = color; rectsArray = rects; isCircle = circle; self = [super initWithFrame:frame]; if (self) { // Initialization code self.opaque = NO; } return self; } - (void)drawRect:(CGRect)rect { [backgroundColor setFill]; UIRectFill(rect); for (NSValue *holeRectValue in rectsArray) { CGRect holeRect = [holeRectValue CGRectValue]; CGRect holeRectIntersection = CGRectIntersection( holeRect, rect ); if (isCircle) { CGContextRef context = UIGraphicsGetCurrentContext(); if( CGRectIntersectsRect( holeRectIntersection, rect ) ) { CGContextAddEllipseInRect(context, holeRectIntersection); CGContextClip(context); CGContextClearRect(context, holeRectIntersection); CGContextSetFillColorWithColor( context, [UIColor clearColor].CGColor ); CGContextFillRect( context, holeRectIntersection); } } else { [[UIColor clearColor] setFill]; UIRectFill(holeRectIntersection); } } }
And in your controller, for example, create a TranparentView with settings:
backgroundColor - the color of the view;
transparentRects - an array of frames;
circle (BOOL) - yes - circle / oval, no - square / rectangle.
So:
NSArray *transparentRects = @[[NSValue valueWithCGRect:CGRectMake(20, 20, 60, 50)]]; TransparentView *transparentView = [[TransparentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) backgroundColor:[UIColor greenColor] transparentRects:transparentRects circle:NO]; [self.view addSubview:transparentView];
You can cut both a circular area and a square one. The code can be improved to suit your needs. Hope this helps you.