CAShapeLayer阴影

时间:2015-02-13 10:24:29

标签: ios iphone cashapelayer

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

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

enter image description here

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

1 个答案:

答案 0 :(得分:15)

非常粗略:

    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)

shot1 shot2

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