SwiftUI Path .closeSubpath() not working the same way as UIBezierPath .close()

寵の児 提交于 2021-01-28 19:19:57

问题


I'm trying to draw the following shape with SwiftUI:

Price tag shape

Before SwiftUI I just had to create a UIBezierPath, add the corners with addArc and then finally call close(), but I don't get the same result when calling closeSubpath on SwiftUI Path.

Here's my code:


            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)

Thanks in advance!


回答1:


It should be drawn via Shape, which specifies rect for path, as in demo below

Note: also fixed clockwise direction

demo

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)
        }
    }
}


来源:https://stackoverflow.com/questions/60564467/swiftui-path-closesubpath-not-working-the-same-way-as-uibezierpath-close

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!