SwiftUI Path .closeSubpath () не работает так же, как UIBezierPath .close () - PullRequest
0 голосов
/ 06 марта 2020

Я пытаюсь нарисовать следующую форму с помощью SwiftUI:

Price tag shape

Перед SwiftUI мне просто нужно было создать UIBezierPath, добавить углы с помощью addAr c, а затем наконец, вызовите close (), но я не получаю тот же результат при вызове closeSubpath для SwiftUI Path.

Вот мой код:


            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)

Заранее спасибо!

1 Ответ

0 голосов
/ 06 марта 2020

Он должен быть нарисован через Shape, который задает прямоугольник для пути, как в демонстрационном примере ниже

Примечание: также фиксированное направление по часовой стрелке

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)
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...