UIView中間透明周圍半透明(四種方法)


 

 

方法一


#import "DrawView.h" @implementation DrawView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //設置 背景為clear self.backgroundColor = [UIColor clearColor]; self.opaque = NO; } return self; } - (void)drawRect:(CGRect)rect { [[UIColor colorWithWhite:0 alpha:0.5] setFill]; //半透明區域 UIRectFill(rect); //透明的區域 CGRect holeRection = CGRectMake(100, 200, 200, 200); /** union: 並集 CGRect CGRectUnion(CGRect r1, CGRect r2) 返回並集部分rect */ /** Intersection: 交集 CGRect CGRectIntersection(CGRect r1, CGRect r2) 返回交集部分rect */ CGRect holeiInterSection = CGRectIntersection(holeRection, rect); [[UIColor clearColor] setFill]; //CGContextClearRect(ctx, <#CGRect rect#>) //繪制 //CGContextDrawPath(ctx, kCGPathFillStroke); UIRectFill(holeiInterSection); }

 

直接添加使用就行

    DrawView *drawView = [[DrawView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:drawView]; 

 

這里寫圖片描述

方法二


#import "DrawViewArc.h" @implementation DrawViewArc - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.opaque = NO; } return self; } - (void)drawRect:(CGRect)rect { //中間鏤空的矩形框 CGRect myRect =CGRectMake(100,100,200, 200); //背景 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:0]; //鏤空 UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:myRect]; [path appendPath:circlePath]; [path setUsesEvenOddFillRule:YES]; CAShapeLayer *fillLayer = [CAShapeLayer layer]; fillLayer.path = path.CGPath; fillLayer.fillRule = kCAFillRuleEvenOdd; fillLayer.fillColor = [UIColor whiteColor].CGColor; fillLayer.opacity = 0.5; [self.layer addSublayer:fillLayer]; } 

 

也是直接調用就行 
這里寫圖片描述

方法三


寫到需要添加 透明圓的 view里

- (void)addArc {
    //中間鏤空的矩形框
    CGRect myRect =CGRectMake(100,100,200, 200); //背景 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:[UIScreen mainScreen].bounds cornerRadius:0]; //鏤空 UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:myRect]; [path appendPath:circlePath]; [path setUsesEvenOddFillRule:YES]; CAShapeLayer *fillLayer = [CAShapeLayer layer]; fillLayer.path = path.CGPath; fillLayer.fillRule = kCAFillRuleEvenOdd; fillLayer.fillColor = [UIColor whiteColor].CGColor; fillLayer.opacity = 0.5; [self.view.layer addSublayer:fillLayer]; } 

 

調用 
[self addArc];

方法四


#import "DrawArc.h" @implementation DrawArc - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.opaque = NO; } return self; } - (void)drawRect:(CGRect)rect { //中間鏤空的矩形框 CGRect myRect =CGRectMake(100,100,200, 200); CGContextRef ctx = UIGraphicsGetCurrentContext(); //背景色 //[[UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]] set]; [[UIColor colorWithWhite:0 alpha:0.5] set]; CGContextAddRect(ctx, rect); CGContextFillPath(ctx); //設置清空模式 /** kCGBlendModeClear, kCGBlendModeCopy, kCGBlendModeSourceIn, kCGBlendModeSourceOut, kCGBlendModeSourceAtop, kCGBlendModeDestinationOver, kCGBlendModeDestinationIn, kCGBlendModeDestinationOut, kCGBlendModeDestinationAtop, kCGBlendModeXOR, kCGBlendModePlusDarker, kCGBlendModePlusLighter */ CGContextSetBlendMode(ctx, kCGBlendModeClear); //畫圓 CGContextAddEllipseInRect(ctx, myRect); //填充 CGContextFillPath(ctx); } 

 

這里寫圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM