Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active December 31, 2020 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/05c1eeec89e7a2838dbe8bc8cb718e7e to your computer and use it in GitHub Desktop.
Save sturdysturge/05c1eeec89e7a2838dbe8bc8cb718e7e to your computer and use it in GitHub Desktop.
EShapeView
extension CGRect {
/// Only necessary for my specific example
/// - Returns: The points we need to create the shape
var eShapePoints: [CGPoint] {
[CGPoint(x: xMultiplied(by: 0), y: yMultiplied(by: 0)),
CGPoint(x: xMultiplied(by: 0), y: yMultiplied(by: 1)),
CGPoint(x: xMultiplied(by: 1), y: yMultiplied(by: 1)),
CGPoint(x: xMultiplied(by: 1), y: yMultiplied(by: 0.8)),
CGPoint(x: xMultiplied(by: 0.2), y: yMultiplied(by: 0.8)),
CGPoint(x: xMultiplied(by: 0.2), y: yMultiplied(by: 0.6)),
CGPoint(x: xMultiplied(by: 0.8), y: yMultiplied(by: 0.6)),
CGPoint(x: xMultiplied(by: 0.8), y: yMultiplied(by: 0.4)),
CGPoint(x: xMultiplied(by: 0.2), y: yMultiplied(by: 0.4)),
CGPoint(x: xMultiplied(by: 0.2), y: yMultiplied(by: 0.2)),
CGPoint(x: xMultiplied(by: 1), y: yMultiplied(by: 0.2)),
CGPoint(x: xMultiplied(by: 1), y: yMultiplied(by: 0))]
}
}
struct EShapeView: View {
var body: some View {
GeometryReader { geometry in
let points = geometry.frame(in: .global).eShapePoints
Path {
path in
path.move(to: points[0])
for point in points {
path.addLine(to: point)
}
}
}
.frame(width: 50, height: 50)
}
}
struct EShape: Shape {
func path(in rect: CGRect) -> Path {
let points = rect.eShapePoints
return Path {
path in
path.move(to: points[0])
for point in points {
path.addLine(to: point)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment