iPhone - Draw Transparent Rectangle on Uiview to Reveal View Beneath

iPhone - Draw transparent rectangle on UIView to reveal view beneath

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

How to draw transparency in UIView?

I'm guessing that your init method is never called (after all, this is not the designated initializer), and so your code never runs and thus the views are created opaque.

how to programmatically in swift show a transparent rectangle on iPhone

Set the backgroundColor of Plot_Demo to .clear:

override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .clear
}

Also, you probably don't want to be calling .draw on a UIView directly -- it'll get drawn automatically when it's added to the view hierarchy. If you want it to draw in a different rectangle, either pass in parameters to it and call setNeedsDisplay or add constraints so that it is the desired size.

Drawing a semi-transparent rectangle in iOS

I guess that the reason why you can't see the transparency of your rect is the presence of a background color in your view.

Would you try setting the view background color like this:

self.backgroundColor = [UIColor clearColor];

BTW, this statement, as well as any statement setting a property of your custom view (e.g., self.alpha = 0.7;) should go into the init method.

EDIT:

as pointed out by @borrrden, the real property to set in order to make the trick is possibly opaque. This is usually set to YES and thus implies the following, according to Apple Docs:

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.

Setting the background color to clearColor will likely set the value of this property to NO as a collateral effect.

EDIT:

I do not understand why you are overriding drawRect in a custom UIView to draw a semi-transparent rectangle, maybe because I do not know what you are trying to do. In any case, an alternative approach to this you might want to look into is using nested UIViews or nested CALayers, where you assign a different alpha/opacity to any of them.

If you are just interested in rectangles with varying degrees of opacity, this would be as simple as nesting UIViews in a parent view and assign the each of them a semi-transparent background color.

can a particular rect of UIview have different alpha..?

Place a custom UIView over the view showing the image, size and position it to exactly overlap.
In the custom view's drawRect method

- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect rBounds = self.bounds;

CGContextRef context = UIGraphicsGetCurrentContext();

// Fill background with 80% white
CGContextSetFillColorWithColor(context, [[[UIColor whiteColor] colorWithAlphaComponent:0.8] CGColor]);
CGContextFillRect(context, rBounds);

// Draw the window 'frame'
CGContextSetStrokeColorWithColor(context, [[UIColor orangeColor] CGColor]);
CGContextSetLineWidth(context, 10);
CGContextStrokeRect(context, self.maskRect);

// make the window transparent
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillRect(context, self.maskRect);
}

where maskRect is the rectangle to be shown transparent.
Do ensure that the background color of the custom view is set to 'clearColor'

You can write any additional logic required to change the maskRect and call 'setNeedsDisplay' on this view.

Drawing a rectangle in UIView


- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect rectangle = CGRectMake(0, 100, 320, 100);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0); //this is the transparent color
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.5);
CGContextFillRect(context, rectangle);
CGContextStrokeRect(context, rectangle); //this will draw the border

}

the effect is like this (backgroundColor is blue)

Sample Image

iPhone - draw a transparent (cleared) rectangle with customized border

Set the stroke color and width as desired, for example:

CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);

If you are also filling the rectangle, do this after filling so the fill doesn't cover up the stroke.

How to get transparency in certain areas of UIView subclass with drawRect override

The view's opaque has to be set to NO and backgroundColor to [UIColor clearColor]. Also, the wrong blend mode was being used. We had:

CGContextSetBlendMode(context, kCGBlendModeSourceAtop);

We needed:

CGContextSetBlendMode(context, kCGBlendModeDestinationOver);

The drawing code was generated by a graphics tool that can output code, and their support department figured out that the wrong blend constant was being generated because of an off-by-one error.



Related Topics



Leave a reply



Submit