swift - 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)
提前致谢!
解决方案
它应该是通过 绘制的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)
}
}
}
推荐阅读
- mysql - 将大数据存储到 MySQL 数据库的最佳实践是什么?
- visual-studio-2015 - Visual Studio 2019 找不到绑定信息,但 VS 2015 很好
- java - 为什么接口有字段并且它们是公开的?
- office-js - Office.context.ui.displayDialogAsync 本地开发
- javascript - 使用 localStorage 隐藏我的工具提示,但在我的脚本中
- android - 内核 (Android) 到用户空间消息多播错误:netlink_broadcast_filtered+0x24/0x3d4
- azure-logic-apps - 如何在 Azure 逻辑应用中使用 Azure Database for PostgreSQL?
- swift - 对计时器进行单元测试?
- c# - C# 反射 typeof(string).GetMethod("Equals) netcoreapp2.2 上的模糊匹配
- regex - 如何使用 htaccess 和正则表达式重定向 URL 的中间部分,使动态端保持原样?