中间透明的 UIView [英] UIView with transparent in middle

查看:42
本文介绍了中间透明的 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建这样一个UIView填充颜色,但中间是透明的?

Is it possible to create such a UIView fill with color, but in the middle is transparent?

我正在考虑在这里创建 5 UIViews.只是想知道是否可以仅使用一个 UIView

I'm thinking about to create 5 UIViews here. Just wondering is it possible to accomplish by using only ONE UIView

推荐答案

Duncan C,我得到知道我应该从哪里开始,然后我找到了 CALayer 中带有透明孔.

From Duncan C, I get to know where should I start, then I found CALayer with transparent hole in it.

UIBezierPath *overlayPath = [UIBezierPath bezierPathWithRect:self.view.bounds];
UIBezierPath *transparentPath = [UIBezierPath bezierPathWithRect:CGRectMake(60, 120, 200, 200)];
[overlayPath appendPath:transparentPath];
[overlayPath setUsesEvenOddFillRule:YES];

CAShapeLayer *fillLayer = [CAShapeLayer layer];
fillLayer.path = overlayPath.CGPath;
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor colorWithRed:255/255.0 green:20/255.0 blue:147/255.0 alpha:1].CGColor;

[self.view.layer addSublayer:fillLayer];

利用2个UIBezierPath,然后填充我想要的颜色(在我的问题中是粉红色),然后添加为子层

Make use of 2 UIBezierPath, then fill with color that I want (in my question is pink color), then add as sublayer

这篇关于中间透明的 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆