2

我正在尝试在 UIButton 上设置嵌入阴影,感谢其他 SO 帖子,我已经设法做到了:

        UIBezierPath *buttonPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.layer.cornerRadius];

        CAShapeLayer* shadowLayer = [CAShapeLayer layer];
        //shadowLayer.cornerRadius = 8.0f;
        [shadowLayer setOpacity:1];
        //[shadowLayer setBackgroundColor:UIColor.redColor.CGColor]; //not working

        // Standard shadow stuff
        [shadowLayer setShadowOpacity:1.0f];
        [shadowLayer setShadowColor:[[UIColor colorWithWhite:1 alpha:1] CGColor]];
        [shadowLayer setShadowOffset:CGSizeMake(2.0f, 2.0f)];
        [shadowLayer setShadowRadius:5];
        // Causes the inner region in this example to NOT be filled.
        [shadowLayer setFillRule:kCAFillRuleEvenOdd];

        // Create the larger rectangle path.
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, CGRectInset(self.bounds, -42.0f, -42.0f));
        // Add the inner path so it's subtracted from the outer path.
        CGPathAddPath(path, NULL, buttonPath.CGPath);
        CGPathCloseSubpath(path);

        [shadowLayer setPath:path];
        CGPathRelease(path);

        [self.layer addSublayer:shadowLayer];

我确实得到了一个漂亮的白色内阴影,但是 shadowLayer 添加了一个黑色不透明矩形(阴影路径)。

尝试过[shadowLayer setFillColor:UIColor.clearColor.CGColor];,但它也消除了阴影。也[shadowLayer setBackgroundColor:UIColor.redColor.CGColor];无所作为。

例子

我怎样才能摆脱它?!

4

1 回答 1

1

添加[self.layer setMasksToBounds:YES];解决了问题!

于 2012-08-15T20:39:47.770 回答