5

我正在尝试为包含图像的 CALayer 创建一个蒙版。该掩码应能够使用多条路径的联合或联合的逆。就可能是分离或联合否认的维恩图而言(在此处查找我无法发布的维恩图图形:https ://en.wikipedia.org/wiki/Logical_connective#Common_logical_connectives )。我还在专门的 iOS 论坛中启动了一个线程,其中包含大量图形和更多示例代码:http ://www.raywenderlich.com/forums/viewtopic.php?f=2&t=7155

我确实成功地创建了第一种情况:多条路径的联合(析取)。

您可以将代码复制到新的单屏应用程序中进行试用(不要忘记包含 Quartz 框架):

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // create a yellow background
    UIView *bg = [[UIView alloc] initWithFrame:self.view.bounds];
    bg.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:bg];

    // create the layer on top of the yellow background that will be masked
    CALayer *imageLayer = [CALayer layer];
    imageLayer.frame = self.view.layer.bounds;
    imageLayer.backgroundColor = [[UIColor blueColor] CGColor];

    // create the mask that will be applied to the layer on top of the
    // yellow background
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillRule = kCAFillRuleEvenOdd;
    maskLayer.frame = self.view.frame;
    CAShapeLayer *maskSubLayer1 = [CAShapeLayer layer];
    maskSubLayer1.fillRule = kCAFillRuleEvenOdd;
    maskSubLayer1.frame = self.view.frame;
    CAShapeLayer *maskSubLayer2 = [CAShapeLayer layer];
    maskSubLayer2.fillRule = kCAFillRuleEvenOdd;
    maskSubLayer2.frame = self.view.frame;

    // add the paths to sublayers of the mask
    maskSubLayer1.path = CGPathCreateWithRect((CGRect){{100, 140}, {100, 150}}, nil);
    maskSubLayer2.path = CGPathCreateWithEllipseInRect((CGRect){{80, 40}, {190, 190}}, nil);
    // add the sublayers to the mask
    // (instead of using CGPathAddPath())
    [maskLayer addSublayer:maskSubLayer1];
    [maskLayer addSublayer:maskSubLayer2];
    // apply the mask to the layer on top of the yellow background
    imageLayer.mask = maskLayer;
    [self.view.layer addSublayer:imageLayer];

}

然而,逆向似乎无法驯服,例如使用蒙面掩码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // create a yellow background
    UIView *bg = [[UIView alloc] initWithFrame:self.view.bounds];
    bg.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:bg];

    // create the layer on top of the yellow background that will be masked
    CALayer *imageLayer = [CALayer layer];
    imageLayer.frame = self.view.layer.bounds;
    imageLayer.backgroundColor = [[UIColor blueColor] CGColor];

    // create the mask that will be applied to the layer on top of the
    // yellow background
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillRule = kCAFillRuleEvenOdd;
    maskLayer.frame = self.view.frame;
    CAShapeLayer *maskSubLayer1 = [CAShapeLayer layer];
    maskSubLayer1.fillRule = kCAFillRuleEvenOdd;
    maskSubLayer1.frame = self.view.frame;
    CAShapeLayer *maskSubLayer2 = [CAShapeLayer layer];
    maskSubLayer2.fillRule = kCAFillRuleEvenOdd;
    maskSubLayer2.frame = self.view.frame;

    // add the paths to sublayers of the mask
    maskSubLayer1.path = CGPathCreateWithRect((CGRect){{100, 140}, {100, 150}}, nil);
    [maskLayer addSublayer:maskSubLayer1];
    maskSubLayer2.path = CGPathCreateWithEllipseInRect((CGRect){{80, 40}, {190, 190}}, nil);
    [maskLayer addSublayer:maskSubLayer2];
    // use the maskLayer to mask the maskBackgroundLayer
    CALayer *maskBackgroundLayer = [CAShapeLayer layer];
    maskBackgroundLayer.frame = self.view.layer.bounds;
    maskBackgroundLayer.backgroundColor = [[UIColor greenColor] CGColor];
    maskBackgroundLayer.mask = maskLayer;

    // apply the maskBackgroundLayer to the layer on top of the yellow background
    imageLayer.mask = maskBackgroundLayer;
    [self.view.layer addSublayer:imageLayer];
    // just for testing: instead of the 2 lines above look at the maskBackgroundLayer
//    [self.view.layer addSublayer:maskBackgroundLayer];

}

我正在寻找如何实现这种效果的想法。

类似的问题是: iOS - 使用 UIBezierPath appendPath 剪切两条路径的联合 (在我的情况下不适用,因为交叉点可能是不规则的) 在 iOS 上结合相交 CGPaths (未解决)

4

0 回答 0