ios添加一个透明视图图使得父视图实现部分透明(在视图上面抠一个透明的洞)

如上图需要在视图中实现中间部分透明实现代码如下:

 

 

创建蒙版视图继承于UIView

#import "LXQuaereEditBoarder.h"

@interface LXQuaereEditBoarder : UIView 

@end

 

 

#import "LXQuaereEditBoarder.m"

@implementation LXQuaereEditBoarder 

- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];

    //w -40

    CGFloat editHeight = ScreenHeight - 116;

    CGFloat editWidth = ScreenWidth;

    CGFloat borderWidth = 2.0;

    CGRect rectBorder;

    

    if (_photo.height == 0 || _photo.width == 0) {

        

    }else if (_photo.height/_photo.width<(editHeight-80)/ScreenWidth) {//宽图

        editHeight = editWidth*_photo.height/_photo.width;

    }else{

        editHeight = ScreenHeight - 116-80;//-80为边距

        editWidth = editHeight/(_photo.height/_photo.width);

    }

   // 大小可以自定义

    rectBorder = CGRectMake((fabs(ScreenWidth-editWidth))/2, (fabs(ScreenHeight - 116-editHeight))/2, editWidth, editHeight);

    

//抠出透明部分

    [[UIColor clearColor] setFill];

    UIRectFill(rectBorder);

    

//画矩形边框可以自定义

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddRect(context, rectBorder);

    [[UIColor whiteColor]setStroke];

    //设置画笔宽度

    CGContextSetLineWidth(context, borderWidth);

    CGContextDrawPath(context, kCGPathStroke);

}

@end

//觉得有用可以点个赞 有疑问可以加qq673219930和楼主一起讨论

posted on 2017-09-07 11:24  jj5200  阅读(985)  评论(0)    收藏  举报

导航