0

我想为弧线创建一个内部阴影,但我得到的是半个圆而不是阴影(见下图)。

我的代码是:

let rect = self.bounds
let segmentArcPath = UIBezierPath()
segmentArcPath.addArc(withCenter: rect.center,
                      radius: (rect.height - insetShadow) / 2,
                      startAngle: -.pi/2,
                      endAngle: value * 2 * .pi - .pi/2,
                      clockwise: true)

shadowPath = segmentArcPath.cgPath

上面代码的当前结果和预期结果

试图添加两条路径(不关闭阴影),但没有成功:

let rect = self.bounds
let segmentArcPath = UIBezierPath()
segmentArcPath.addArc(withCenter: rect.center,
                      radius: (rect.height - insetShadow) / 2,
                      startAngle: internalStrokeStart,
                      endAngle: internalStrokeEnd,
                      clockwise: true)

let segmentArcPath2 = UIBezierPath()
segmentArcPath2.addArc(withCenter: rect.center,
                      radius: (rect.height - insetShadow) / 2 - 5,
                      startAngle: internalStrokeStart,
                      endAngle: internalStrokeEnd,
                      clockwise: false)

segmentArcPath.append(segmentArcPath2)
shadowPath = segmentArcPath.cgPath

如何创建如上图所示的阴影?你有什么提示吗?

4

1 回答 1

0

正如@vacawama 所推荐的:

只创建一个 UIBezierPath。画完外圆弧后,向圆弧中心画一条 5 单位的线,然后调用 addArc 将内圆弧添加到同一路径

let rect = self.bounds
let outerRadius = (rect.height - insetShadow) / 2
let segmentArcPath = UIBezierPath()
segmentArcPath.addArc(withCenter: rect.center,
                      radius: outerRadius,
                      startAngle: internalStrokeStart,
                      endAngle: internalStrokeEnd,
                      clockwise: true)
let innerCircleRadius = outerRadius - 2
let startPointXInnerCircle = cos(internalStrokeEnd) * innerCircleRadius + rect.center.x
let startPointYInnerCircle = sin(internalStrokeEnd) * innerCircleRadius + rect.center.y

segmentArcPath.addLine(to: CGPoint(x: startPointXInnerCircle, y: startPointYInnerCircle))

segmentArcPath.addArc(withCenter: rect.center,
                      radius: innerCircleRadius,
                      startAngle: internalStrokeEnd,
                      endAngle: internalStrokeStart,
                      clockwise: false)

shadowPath = segmentArcPath.cgPath
于 2020-08-24T18:07:54.627 回答