create quarter transparent hole at right bottom on

2019-02-15 13:27发布

问题:

Hi i want to create a quarter transparent hole at right bottom on overlay UIView.

i am able to solve it using below code. But it does not look right as i am creating a rectangle outside the bond of view.

What i have tried:

@implementation PartialTransparentView

- (id)initWithBottomRightCornerRadiusForView:(UIView *)view   withRadius:(CGFloat)radius
{
[self commonInitWithRect:CGRectMake(view.frame.size.width - radius,  view.frame.size.height - radius, radius*2, radius*2)];
self = [super initWithFrame:CGRectMake(0, 0, 5000, 5000)];//**it does not look right to me**
if (self) {
    // Initialization code
    self.opaque = NO;
}
return self;
}

-(void)commonInitWithRect:(CGRect)rect{
    backgroundColor = [UIColor colorWithWhite:1 alpha:0.75];
    rectToBeSurrounded = rect;
}
- (void)drawRect:(CGRect)rect {

    [backgroundColor setFill];
    UIRectFill(rect);



        CGFloat x = rectToBeSurrounded.origin.x;
        CGFloat y = rectToBeSurrounded.origin.y;

        CGFloat width = rectToBeSurrounded.size.width;
        CGFloat height = rectToBeSurrounded.size.height;

        //create outer square
        CGFloat outerX = (x - width/2);
        CGFloat outerY = y - height/2;
        CGFloat outerWidth = 2*width;
        CGFloat outerHeight = outerWidth;
        //create outer square

        CGRect outerRect = CGRectMake(outerX, outerY, outerWidth, outerHeight);

        CGRect holeRectIntersection = CGRectIntersection( outerRect, rect );

        CGContextRef context = UIGraphicsGetCurrentContext();

        if( CGRectIntersectsRect( holeRectIntersection, rect ) )
        {
            CGContextAddEllipseInRect(context, holeRectIntersection);
            CGContextClip(context);
            CGContextClearRect(context, holeRectIntersection);
            CGContextSetFillColorWithColor( context, [UIColor clearColor].CGColor );
            CGContextFillRect( context, holeRectIntersection);
        }
}

Now i am using above code as :

PartialTransparentView *transparentView = [[PartialTransparentView alloc] initWithBottomRightCornerRadiusForView:self.view withRadius:50];
[self.view addSubview:transparentView];

Result as expected:

i know my solution will break if i have to acheive same thing but on top left of view. what i am looking for just provide center (x, y) and radius for circle and get desired results.

Thanks Basd on Mr.T

UIView *transparentView = [[UIView alloc] initWithFrame:self.view.frame];
    [transparentView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.75]];
    [self.view addSubview:transparentView];

    circleView *acircleView = [[circleView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)];
    [acircleView setBackgroundColor:[UIColor grayColor]];
    [transparentView addSubview:acircleView];

and circleView.m

- (void)drawRect:(CGRect)rect {
    // Drawing code
    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(50, 50, 60, 60)];
    [UIColor.grayColor setFill];
    [ovalPath fill];
}

output:

回答1:

My suggestions is to add the transparent view as a separate view on your view controller. This can be either done on storyboard,so that you can set the background color and the alpha value to give the transparent effect!!!

Now create another view to make the circle and add it to transparent view, , and move this view on the transparent view according to your need!!!

Create the circle using bezier path:

circleView.m

 - (void)drawRect:(CGRect)frame {
    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), 60, 60)];
    [UIColor.grayColor setFill];
    [ovalPath fill];
}

For testing purpose, I have created a circle view on my IB and created an outlet property in my view controller.

HEre is the screenshot.

Now to move the circle, I can simply change the frame of the circle view, wherever I need.

For example, If I want to move it to top left, I simply do:

-(void)moveCircleViewwithX:(float) x withY:(float) y{

    _cView.frame=CGRectMake(x, y, _cView.frame.size.width, _cView.frame.size.height);


}

The result will be:

Update

Put the following the drawRect method of transparentView:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect transparentPart = self.seeRect;           //this is the rect of the circle view
CGContextAddEllipseInRect(ctx,transparentPart);  //make the circle shape
CGContextClip(ctx);
CGContextClearRect(ctx, transparentPart);

and in your view controller:

when you want to apply the mask i.e the transparent for both circle and for the transparent layer:

-(void)applyMask{

    [_cView setCircleColor:[UIColor clearColor]];   //circle view bezier path color
    [_cView setNeedsDisplay];
    [_tView setSeeRect:_cView.frame];    //set the transparency cut on transparency view
    [_tView setNeedsDisplay];


}

Once you do this, you will get the transparency view!!!

You can move the circle by simply calling

      [self moveCircleViewwithX:-30 withY:10];   //top left corner

and you can apply the transparency mask by simply calling:

      [self applyMask];

So, the final result after you call the applyMask method will be: