我正在使用SwiftUI,需要实现模仿特定设计的自定义Shape.形状是长方形,但有明显的特征:上角是圆角的,下边缘类似于一排圆角三角形,看起来就像撕破的纸.
有人可以指导我如何创建这个特定的形状,或者建议一种更好的方法来实现SwiftUI中矩形底部边缘的撕纸效果?
以下是我到目前为止try 过的:
struct CustomShape: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
let width = rect.size.width
let height = rect.size.height
let bottomLeft = CGPoint(x: 0, y: height)
let bottomRight = CGPoint(x: width, y: height)
let topLeft = CGPoint(x: 0, y: 0)
let topRight = CGPoint(x: width, y: 0)
path.move(to: bottomRight)
path.addLine(to: CGPoint(x: bottomRight.x, y: topRight.y))
path.addLine(to: CGPoint(x: topLeft.x, y: topLeft.y))
path.addLine(to: CGPoint(x: bottomLeft.x, y: bottomLeft.y))
let triangleWidth: CGFloat = 11
let triangleHeight: CGFloat = 6
var x: CGFloat = 0
while x <= rect.width {
let startX = x
let endX = x + triangleWidth
let midX = (startX + endX) / 2
path.move(to: CGPoint(x: startX, y: rect.maxY))
path.addLine(to: CGPoint(x: midX, y: rect.maxY - triangleHeight))
path.addLine(to: CGPoint(x: endX, y: rect.maxY))
x += triangleWidth
}
return path
}
}