问题描述
嗨,我想在Overlay 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];
结果是预期的:
推荐答案
我的建议是在视图控制器上添加透明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上创建了一个圆形视图,并在视图控制器中创建了一个插座属性.
这是屏幕截图.
现在要移动圆,我可以简单地更改圆圈view的框架.
例如,如果我想将其移至左上,我只是做:
-(void)moveCircleViewwithX:(float) x withY:(float) y{ _cView.frame=CGRectMake(x, y, _cView.frame.size.width, _cView.frame.size.height); }
结果将是:
更新
放置以下透明视图的绘制方法:
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方法后的最终结果是: