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

本文介绍了一种在iOS应用中实现视图中间部分透明的方法,通过创建自定义UIView子类来绘制特定区域的透明效果及边框。适用于需要自定义视图显示效果的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 

创建蒙版视图继承于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和楼主一起讨论

转载于:https://www.cnblogs.com/hexuejie/p/7488719.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值