1. 程式人生 > >UIView中間透明周圍半透明(四種方法)

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

}

這裡寫圖片描述