简体   繁体   English

CAShapeLayer阴影

[英]CAShapeLayer shadow

Given the following CAShapeLayer is it possible to add a drop shadow like the following image at the tip? 鉴于以下CAShapeLayer是否可以在提示处添加如下图所示的阴影?

I'm using a UIBezierPath to draw the bars. 我正在使用UIBezierPath绘制条形图。

在此输入图像描述

- (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;
}

It will need to be applied to this UIBezierPath : 它需要应用于此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;
}

Very roughly: 非常粗略:

    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)

SHOT1SHOT2

I think you need two arcs, and one circle for cap with shadow. 我认为你需要两个圆弧,一个圆圈用于带阴影的圆顶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM