首页 > 解决方案 > SwiftUI Path .closeSubpath() 与 UIBezierPath .close() 的工作方式不同

问题描述

我正在尝试使用 SwiftUI 绘制以下形状:

价格标签形状

在 SwiftUI 之前,我只需要创建一个 UIBezierPath,使用 addArc 添加角,然后最后调用 close(),但是在 SwiftUI Path 上调用 closeSubpath 时我没有得到相同的结果。

这是我的代码:


            Path { path in
                let width: CGFloat = 23
                let height: CGFloat = 24
                let arrowWidth = height / 2.0
                let cornerRadius = height / 7.5

                path.addArc(center: CGPoint(x: width - cornerRadius, y: cornerRadius), radius: cornerRadius, startAngle: Angle(degrees: -90), endAngle: .zero, clockwise: true)
                path.addLine(to: CGPoint(x: width, y: height - cornerRadius))
                path.addArc(center: CGPoint(x: width - cornerRadius, y: height - cornerRadius), radius: cornerRadius, startAngle: .zero, endAngle: Angle(degrees: 90), clockwise: true)
                path.addArc(center: CGPoint(x: arrowWidth + cornerRadius, y: height - cornerRadius), radius:  cornerRadius, startAngle: Angle(degrees: 90), endAngle: Angle(degrees: 135), clockwise: true)
                path.addArc(center: CGPoint(x: cornerRadius, y: height / 2.0), radius: cornerRadius, startAngle: Angle(degrees: 135), endAngle: Angle(degrees: 225), clockwise: true)
                path.addArc(center: CGPoint(x: arrowWidth + cornerRadius, y: cornerRadius), radius: cornerRadius, startAngle: Angle(degrees: 225), endAngle: Angle(degrees: -90), clockwise: true)
                path.closeSubpath()
            }
            .foregroundColor(.red)

提前致谢!

标签: swiftswiftui

解决方案


它应该是通过 绘制的Shape,它为路径指定了矩形,如下面的演示

注意:也固定顺时针方向

演示

struct NewShape_Previews: PreviewProvider {
    static var previews: some View {
        NewShape()
            .frame(width: 100, height: 48)
            .foregroundColor(Color.red)
    }
}

struct NewShape: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            let width: CGFloat = rect.width
            let height: CGFloat = rect.height
            let arrowWidth = height / 2.0
            let cornerRadius = height / 7.5

            path.addArc(center: CGPoint(x: width - cornerRadius, y: cornerRadius), radius: cornerRadius, startAngle: Angle(degrees: -90), endAngle: .zero, clockwise: false)
            path.addLine(to: CGPoint(x: width, y: height - cornerRadius))
            path.addArc(center: CGPoint(x: width - cornerRadius, y: height - cornerRadius), radius: cornerRadius, startAngle: .zero, endAngle: Angle(degrees: 90), clockwise: false)
            path.addArc(center: CGPoint(x: arrowWidth + cornerRadius, y: height - cornerRadius), radius:  cornerRadius, startAngle: Angle(degrees: 90), endAngle: Angle(degrees: 135), clockwise: false)
            path.addArc(center: CGPoint(x: cornerRadius, y: height / 2.0), radius: cornerRadius, startAngle: Angle(degrees: 135), endAngle: Angle(degrees: 225), clockwise: false)
            path.addArc(center: CGPoint(x: arrowWidth + cornerRadius, y: cornerRadius), radius: cornerRadius, startAngle: Angle(degrees: 225), endAngle: Angle(degrees: -90), clockwise: false)
        }
    }
}

推荐阅读