A library for quickly and easily drawing and modifying complex shapes.
To try out this project, open the DrawingShapes.xcodeproj file, then run the Testing playground. To see the outputs of the various shape statements, be sure to expand the preview on the right side of the playground.
Here are a few examples of some fun quick shapes you can make with this library.
Firstly lets start off by making a square:
let square = Shape().to(0, 0).to(0, 100).to(100, 100).to(100, 0).closed().withFill(.white)
let cube = square.prism()
Furthermore, if we want to apply more advanced styling, we can apply a gradient and show the points of the shape.
let styledCube = cube.withFill(.argon).showingPoints()
Now let's look at some more complex shapes, such as a pyramid.
To make a pyramid, it is almost as easy as making a square but with one additional call to joinedAt
.
let pyramid = Shape()
.to(0, 100).to(100, 100).to(125, 75).to(25, 75).closed()
.joinedAt(x: 60, y: -30)
.withFill(.wireTap).showingPoints()
To make a bipyramid, we can do this in a few different ways. The first way we can do this is by using the existing pyramid and mirroring it about its bottom axis and performing a translation.
let bipyramid = squareBipyramid + squareBipyramid.reflected(over: .bottom).flipped(.vertically).translatedBy(x: 0, y: -25)
Alternatively, we can make the new bipyramid from scratch. This can be done by adding a slight modification to the code that generates the pyramid, just by passing an additional point to the joinedAt
method.
let bipyramid = Shape()
.to(0, 100).to(100, 100).to(125, 75).to(25, 75).closed()
.joinedAt(points: (60, -30), (65, 205))
.withFill(.wireTap).showingPoints()
As we can see, this produces the same result:
Polygon:
var polygonalPrism = Shape.regularPolygon(n: 7, sideLength: 100).prism()
polygonalPrism.applyingOptions(.fill(.electricViolet), .showPoints)
Badge:
let badge = Shape.star(numberOfPoints: 9, radiusPercent: 0.7).withFill(.red).applyingOption(.fillInShape(true))
21-pointed star:
let star = Shape.crossStar(numberOfPoints: 21).applyingOptions(.fill(.timber), .lineWidth(1.0)).scaled(by: 3)
You can interact with these shapes and use them in your own project in two primary ways.
-
Extracting a
UIView
from a shape- This can be done by calling
shape.draw()
- This can be done by calling
-
Extracting a
UIBezierPath
from a shape- This can be done by calling
shape.path
- This can be done by calling