在覆盖UIView上的右下角创建四分之一透明孔

嗨我想在叠加UIView的右下角创建一个四分之一透明孔。

我可以使用下面的代码解决它。 但它看起来不正确,因为我在视图之外创建了一个矩形。

我尝试过的:

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

现在我使用上面的代码:

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

结果如预期:

OUTPUT

我知道我的解决方案会破坏,如果我必须做同样的事情,但在视图的左上角。 我正在寻找的只是为圆提供中心(x,y)和半径,并获得所需的结果。

谢谢Bas先生

 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]; 

和circleView.m

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

输出: 不要得到圆圈,而是我变得正方形

我的建议是将透明view添加为视图控制器上的单独view 。 这可以在storyboard完成,这样你就可以设置背景颜色和alpha值来提供透明效果!

现在创建另一个view来制作圆圈并将其添加到透明view中,并根据您的需要在透明view上移动此view

使用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]; } 

出于测试目的,我在IB上创建了一个圆形视图,并在视图控制器中创建了一个outlet属性。

HEre是截图。

在此处输入图像描述

现在移动圆圈,我可以简单地改变圆形view的框架,无论我需要什么。

例如,如果我想将其移到左上角,我只需:

 -(void)moveCircleViewwithX:(float) x withY:(float) y{ _cView.frame=CGRectMake(x, y, _cView.frame.size.width, _cView.frame.size.height); } 

结果将是:

在此处输入图像描述

更新

将下面的transparentView的drawRect方法放入:

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

并在您的视图控制器中:

当你想要应用蒙版时,即圆圈和透明层的透明度:

 -(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]; } 

一旦你这样做,你将获得透明度视图!

您可以通过简单地调用来移动圆圈

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

您只需调用以下命令即可应用透明蒙版:

  [self applyMask]; 

因此,调用applyMask方法后的最终结果将是:

在此处输入图像描述