9

CAGradientLayer用 a掩盖 aCAShapeLayer为了生成位于我表中某些行内的书签。

在此处输入图像描述

        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddLines(path,
                       NULL,
                       (CGPoint[]){
                           CGPointMake(8, 0),
                           CGPointMake(22, 0),
                           CGPointMake(22, 22),
                           CGPointMake(15, 16),
                           CGPointMake(8, 22)
                       },
                       5);
        CGPathCloseSubpath(path);

        CAShapeLayer *mask = [CAShapeLayer layer];
        mask.fillColor = [UIColor redColor].CGColor;
        mask.bounds = CGRectMake(0, 0, 30, 30);
        mask.path = path;
        mask.anchorPoint = CGPointMake(0, 0);

        CGPathRelease(path);

        CAGradientLayer *favoriteBadge = [CAGradientLayer layer];
        favoriteBadge.colors =
            @[(id)[UIColor colorWithRed:.97f green:0.5f blue:0.1f alpha:1.f].CGColor, // orange
              (id)[UIColor colorWithRed:1.f green:.8f blue:0.f alpha:1.f].CGColor]; // yellow
        favoriteBadge.locations = @[@0.0, @1.0];
        favoriteBadge.bounds = CGRectMake(0, 0, 30, 30);
        favoriteBadge.anchorPoint = CGPointMake(1, 0);
        favoriteBadge.position = CGPointMake(self.bounds.size.width, 0);
        favoriteBadge.mask = mask;
        // FIXME: This shadow is not being applied
        favoriteBadge.shadowColor = [UIColor colorWithRed:.97f green:0.5f blue:0.1f alpha:1.f].CGColor;
        favoriteBadge.shadowOpacity = 1.f;

这看起来太平了,所以我想在形状本身周围加上一个阴影来完成这个。我试过在图层上启用阴影,但似乎没有应用。我假设它被应用于不在形状内部的区域。如何为形状本身添加阴影?

我可以复制图层,将其变暗并将其位置偏移几个像素,但是当 CoreAnimation 可能对我来说更有效时,这感觉很浪费。

4

2 回答 2

10

为您的遮罩层创建一个容器层,并将阴影应用到容器层。我相信这里是这样描述的:

http://travisjeffery.com/b/2012/08/ios-how-to-mask-and-shadow-an-image/

于 2013-02-03T11:00:09.570 回答
-2

也许你唯一错过的就是

favoriteBadge.masksToBounds = NO
于 2013-05-08T15:18:22.233 回答