【发布时间】:2013-05-06 22:31:29
【问题描述】:
我有一个简单的视图(图片的左侧),我需要为此视图创建某种叠加层(图片的右侧)。这个覆盖应该有一些不透明度,所以它下面的视图仍然是部分可见的。 最重要的是,这个覆盖层的中间应该有一个圆孔,这样它就不会覆盖视图的中心(见下图)。
我可以像这样轻松地创建一个圈子:
int radius = 20; //whatever
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,radius,radius) cornerRadius:radius].CGPath;
circle.position = CGPointMake(CGRectGetMidX(view.frame)-radius,
CGRectGetMidY(view.frame)-radius);
circle.fillColor = [UIColor clearColor].CGColor;
还有一个像这样的“完整”矩形覆盖:
CAShapeLayer *shadow = [CAShapeLayer layer];
shadow.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height) cornerRadius:0].CGPath;
shadow.position = CGPointMake(0, 0);
shadow.fillColor = [UIColor grayColor].CGColor;
shadow.lineWidth = 0;
shadow.opacity = 0.5;
[view.layer addSublayer:shadow];
但我不知道如何将这两个图层组合起来,以便它们产生我想要的效果。任何人?我什么都试过了...非常感谢您的帮助!
【问题讨论】:
-
你能创建一个包含矩形和圆形的贝塞尔曲线,然后在绘制过程中使用的缠绕规则会创建一个孔(我没有尝试过)。
-
我不知道该怎么做:)
-
用矩形创建,然后
moveToPoint,然后添加圆角矩形。检查文档以了解UIBezierPath提供的方法。 -
看看这个类似的问答是否有帮助:[Cut transparent hole in UIView][1] [1]:stackoverflow.com/questions/9711248/…
-
在这里查看我的解决方案:stackoverflow.com/questions/14141081/… 希望这对某人有所帮助
标签: ios objective-c calayer quartz-core