【问题标题】:Add a shadow to CAShapeLayer, so that the inside remains transparent给 CAShapeLayer 添加阴影,使内部保持透明
【发布时间】:2013-05-08 15:21:11
【问题描述】:

我想为路径添加发光效果,例如 (OS X) 界面元素获得焦点时周围的蓝色发光。

我使用了带有(矩形)路径的 CAShapeLayer:

self.borderLayer = [CAShapeLayer layer];
CGPathRef path = CGPathCreateWithRect(self.bounds, NULL);
[self.borderLayer setPath:path];
CGPathRelease(path);

最后,这给了我一个透明的 UIView,周围有边框。 (在我的具体情况下,它是一条带有附加动画的虚线,但这对于这个特定问题并不重要)

我玩过 CALayer 的阴影属性,但它们总是会填满整个图层。

self.borderLayer.shadowPath = self.borderLayer.path;
self.borderLayer.shouldRasterize = YES;

我想要的是只有 UIViews 周围的线会投下阴影,这样 UIView 的内部就保持透明。

【问题讨论】:

    标签: iphone ios objective-c core-animation


    【解决方案1】:

    我在看到我想要的阴影而不是发光的地方时遇到了类似的问题。我通过使用两个 CALayers 解决了它。一,在代码中,“_bg”作为背景(在我的例子中为黑色,不透明度为 0.55)和白色边框。代码“_shadow”中的另一层具有清晰的背景并添加了发光效果。 _bg 是 _shadow 层的子视图。以下是相关代码:

    _bg = [CALayer layer];
    _shadow = [CALayer layer];
    
    [self.layer insertSublayer:_shadow atIndex:0];
    [_shadow addSublayer:_bg];
    
    _bg.frame = self.bounds;
    _bg.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:.55].CGColor;
    _bg.cornerRadius=20.0;
    _bg.borderColor=[UIColor whiteColor].CGColor;
    _bg.borderWidth=2.0;
    
    _shadow.frame=self.bounds;
    _shadow.masksToBounds=NO;
    _shadow.backgroundColor = [UIColor clearColor].CGColor;
    _shadow.cornerRadius=3.0;
    _shadow.shadowRadius=3.0;
    _shadow.shadowColor=[UIColor whiteColor].CGColor;
    _shadow.shadowOpacity=0.6;
    _shadow.shadowOffset=CGSizeMake(0.0, 0.0);
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

          //background layer of the shadow layer
          contentViewBackgroundLayer = [CALayer layer];
          contentViewBackgroundLayer.frame = contentView.bounds;
          contentViewBackgroundLayer.backgroundColor = someColor.CGColor;
          //shadow layer
          contentViewShadowLayer = [CALayer layer];
          [contentViewShadowLayer addSublayer:contentViewBackgroundLayer];
          contentViewShadowLayer.backgroundColor = [UIColor clearColor].CGColor;
          //shadowRadius
          contentViewShadowLayer.shadowRadius = 10.0;
          contentViewShadowLayer.shadowColor = [UIColor blackColor].CGColor;
          contentViewShadowLayer.shadowOpacity = 0.5;
          contentViewShadowLayer.shadowOffset = CGSizeMake(0.0, 0.0);
          //Important!!!!  add mask to shadowLayer
          contentViewBackgroundLayer.frame = contentView.bounds;
          contentViewShadowLayer.frame = contentView.bounds;
          CGRect rect = CGRectMake(contentViewShadowLayer.bounds.origin.x - 20, contentViewShadowLayer.bounds.origin.y - 20, contentViewShadowLayer.bounds.size.width + 40, contentViewShadowLayer.bounds.size.height + 40);
          UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
          [path appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, contentView.bounds.size.width, contentView.bounds.size.height)]];
          //a rectangle which is transparent surrounded by a bigger rectangle
          CAShapeLayer *shapeLayer = [CAShapeLayer layer];
          shapeLayer.fillRule = kCAFillRuleEvenOdd;
          shapeLayer.path = [path CGPath];
          contentViewShadowLayer.mask = shapeLayer;
          [contentView.layer insertSublayer:contentViewShadowLayer atIndex:0];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-24
        • 1970-01-01
        • 2012-06-07
        • 1970-01-01
        • 2021-02-04
        • 1970-01-01
        • 1970-01-01
        • 2016-10-06
        相关资源
        最近更新 更多