CAShapeLayer阴影 [英] CAShapeLayer shadow

查看:710
本文介绍了CAShapeLayer阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下CAShapeLayer是否可以在提示处添加如下图所示的投影?

Given the following CAShapeLayer is it possible to add a drop shadow like the following image at the tip?

我正在使用 UIBezierPath 绘制条形图。

I'm using a UIBezierPath to draw the bars.

- (CAShapeLayer *)gaugeCircleLayer {

    if (_gaugeCircleLayer == nil) {

        _gaugeCircleLayer = [CAShapeLayer layer];
        _gaugeCircleLayer.lineWidth = self.gaugeWidth;
        _gaugeCircleLayer.fillColor = [UIColor clearColor].CGColor;
        _gaugeCircleLayer.strokeColor = self.gaugeTintColor.CGColor;
        _gaugeCircleLayer.strokeStart = 0.0f;
        _gaugeCircleLayer.strokeEnd = self.value;
        _gaugeCircleLayer.lineCap = kCALineCapRound;
        _gaugeCircleLayer.masksToBounds = NO;
        _gaugeCircleLayer.cornerRadius = 8.0;
        _gaugeCircleLayer.shadowRadius = 8.0;
        _gaugeCircleLayer.shadowColor = [UIColor blackColor].CGColor;
        _gaugeCircleLayer.shadowOpacity = 0.5;
        _gaugeCircleLayer.shadowOffset = CGSizeMake(0.0, 0.0);
        _gaugeCircleLayer.path = [self circlPathForCurrentGaugeStyle].CGPath;
    }

    return _gaugeCircleLayer;
}

它需要应用于此 UIBezierPath

- (UIBezierPath *)insideCirclePath {

    CGPoint arcCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:arcCenter
                                                        radius:CGRectGetWidth(self.bounds) / 2.0f
                                                    startAngle:(3.0f * M_PI_2)
                                                      endAngle:(3.0f * M_PI_2) + (2.0f * M_PI)
                                                     clockwise:YES];


    _titleTextLabel.textColor = self.gaugeTintColor;

    return path;
}


推荐答案

非常粗略:

    let arc1 = CAShapeLayer()
    arc1.lineWidth = 20.0
    arc1.path = UIBezierPath(ovalInRect: CGRectMake(10, 10, 80, 80)).CGPath
    arc1.strokeStart = 0
    arc1.strokeEnd = 0.5
    arc1.strokeColor = UIColor.grayColor().CGColor
    arc1.fillColor = UIColor.clearColor().CGColor
    layer.addSublayer(arc1)

    let cap = CAShapeLayer()
    cap.shadowColor = UIColor.blackColor().CGColor
    cap.shadowRadius = 8.0
    cap.shadowOpacity = 0.9
    cap.shadowOffset = CGSize(width: 0, height: 0)
    cap.path = UIBezierPath(ovalInRect: CGRectMake(0, 40, 20, 20)).CGPath
    cap.fillColor = UIColor.grayColor().CGColor
    layer.addSublayer(cap)

    let arc2 = CAShapeLayer()
    arc2.lineWidth = 20.0
    arc2.path = UIBezierPath(ovalInRect: CGRectMake(10, 10, 80, 80)).CGPath
    arc2.strokeStart = 0.5
    arc2.strokeEnd = 1.0
    arc2.strokeColor = UIColor.grayColor().CGColor
    arc2.fillColor = UIColor.clearColor().CGColor
    layer.addSublayer(arc2)

我认为你需要两个圆弧,一个圆圈带阴影。

I think you need two arcs, and one circle for cap with shadow.

这篇关于CAShapeLayer阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆