UIViewController.view.superview 自定义形状

UIViewController.view.superview custom shape

我在 iPad 上有一个显示为自定义形状的控制器。实际上我已经使视图本身透明了,但是它的子视图对用户是可见的。 我在 viewWillLayoutSubviews 方法中使用以下代码来制作一个具有小矩形形式的控制器:

self.view.backgroundColor = [UIColor clearColor];
self.view.superView.backgroundColor = [UIColor clearColor];
self.view.superview.bounds =...

但是如果我不想要矩形怎么办?有没有办法让 CGPath 不为此目的而令人费解?

在这种情况下,您必须创建一个具有所需形状的 CAShapeLayer,然后使用您创建的 CAShapeLayer 遮盖视图的图层。例如,带有圆形遮罩的视图:

UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
                                                     radius:75
                                                 startAngle:0
                                                   endAngle:M_PI * 2
                                                  clockwise:YES];


CAShapeLayer *lm = [[CAShapeLayer alloc] init];
lm.path = aPath.CGPath;
self.view.layer.mask = lm;