ios - CALayer 创建透明圆形 mask

标签 ios iphone quartz-core

我想创建一个 mask 层,它是一个圆圈,使圆圈内容透明并保持周围的一切。不幸的是,下面的代码恰恰相反,它画了一个圆圈并使周围的一切都变得透明。

CAShapeLayer * shape = [CAShapeLayer layer];
shape.frame          = CGRectMake((CGSSize().width/2.f)-40.f, -40.f, 80.f, 80.f);

CGPathRef pathRef    =
CGPathCreateWithEllipseInRect(CGRectMakeBoundsWithSize(shape.frame.size), NULL);

shape.path            = pathRef;
shape.fillColor       = [UIColor blueColor].CGColor;

self.layer.mask = shape;

最佳答案

是的,kCAFillRuleEvenOdd 在不先添加一个矩形的情况下并没有发挥它的魔力,不过这里有一个工作片段:

CAShapeLayer *shape = [CAShapeLayer layer];

shape.frame = self.bounds;

CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathAddRect(pathRef, NULL, self.bounds);
CGPathAddEllipseInRect(pathRef, NULL, self.bounds);

shape.fillRule = kCAFillRuleEvenOdd;
shape.path = pathRef;

self.layer.mask = shape;

关于ios - CALayer 创建透明圆形 mask ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22019122/

相关文章:

ios - 在 iOS 中,初始 View Controller 始终与 Root View Controller 相同

ios - 如何为 iPad 应用程序创建甘特图 View ?

ios - 空 CGMutablePathRef 路径?

iphone - 我可以在 iOS 中运行套接字服务器吗?

ios - 在同一 View Controller 中的另一个 UIView 后面进行 UIView 更新

ios - Swift 计算属性返回值

ios - 如何在用户注销后正确释放 ViewControllers?

ios - 无法在 iOS 上的 Azure NotificationHub 中注册设备,无回调

ios - 数据未插入 sqliteDB iOS 中

java - iOS 应用程序中的 JVM 可以运行 Android 应用程序吗?