通过组合多个路径创建CALayer掩码

时间:2013-04-17 14:25:47

标签: calayer uibezierpath cgpath quartz-core cashapelayer

我正在尝试为包含图像的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 - Clipping union of two paths using UIBezierPath appendPath (不适用于我的情况,因为交叉路口可能不规则) Combining Intersecting CGPaths on iOS (未解析)

0 个答案:

没有答案