UIView镂空

编程入门 行业动态 更新时间:2024-10-26 12:27:27

<a href=https://www.elefans.com/category/jswz/34/1696424.html style=UIView镂空"/>

UIView镂空

在UIView中镂空其中一块的方法有两种:

- (instancetype)initWithFrame:(CGRect)frame  {self = [super initWithFrame:frame];if (self) {self.backgroundColor = [UIColor clearColor];self.opaque = NO;}return self;
}- (void)drawRect:(CGRect)rect {[super drawRect:rect];CGRect clearRect = CGRectMake(100, 100, 100, 100);CGContextRef ctx = UIGraphicsGetCurrentContext();[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8] set];CGContextAddRect(ctx, self.frame);CGContextFillPath(ctx);
// 将混合模式选择为清除CGContextSetBlendMode(ctx, kCGBlendModeClear);CGContextAddRect(ctx, clearRect);CGContextFillPath(ctx);// 也可直接清除,因为将混合模式设置为清除的话,整个clearRect就会被清除,想要添加边界做成微信扫描的效果比较困难。
// CGClearRect(ctx, clearRect);
}
复制代码
- (instancetype)initWithFrame:(CGRect)frame  {self = [super initWithFrame:frame];if (self) {self.backgroundColor = [UIColor clearColor];self.opaque = NO;}return self;
}- (void)drawRect:(CGRect)rect {[super drawRect:rect];CGRect clearRect = CGRectMake(100, 100, 100, 100);UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:clearRect];[path appendPath:rectPath];
// 设置填充规则[path setUsesEvenOddFillRule:YES];CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;shapeLayer.path = path.CGPath;shapeLayer.fillRule = kCAFillRuleEvenOdd;shapeLayer.opacity = 0.5;[self.layer addSublayer:shapeLayer];
}复制代码

转载于:

更多推荐

UIView镂空

本文发布于:2024-02-26 09:47:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1702078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UIView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!