EasierPath is a library to make UIBezierPath easier to use. More specifically, you can write more intuitive and concise code when you draw a straight line or a curve.
If you draw the shape of a jar using UIBezierPath, you will write the code as below.
let bezierPath:UIBezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 100, y: 100))
bezierPath.addLine(to: CGPoint(x: 200, y: 100))
bezierPath.addCurve(to: CGPoint(x: 200, y: 300), controlPoint1: CGPoint(x: 250, y: 150), controlPoint2: CGPoint(x: 175, y: 250))
bezierPath.addLine(to: CGPoint(x:100,y:300))
bezierPath.addCurve(to: CGPoint(x: 100, y: 100), controlPoint1: CGPoint(x: 125, y: 250), controlPoint2: CGPoint(x: 50, y: 150))
bezierPath.close()
let caShapeLayer:CAShapeLayer = CAShapeLayer()
caShapeLayer.path = bezierPath.cgPath
caShapeLayer.strokeColor = UIColor.white.cgColor
caShapeLayer.fillColor = UIColor.systemPink.cgColor
caShapeLayer.lineWidth = 3
view.layer.addSublayer(caShapeLayer)
Before drawing a line or curve, I had to remember where it ended and set the point accordingly.
The more we drew this, the more we had to remember, and it was so unclear to check through the code.
Each line or the curved point is drawn based on the starting point, so if the starting point changed, you had to change the points of all the lines.
If you didn't want to do that, you could have used a certain variable to put the value and then designated it, but this was also a hassle.
In order to display UIBezierPath in View, there was a hassle of applying the path created by creating CAShapeLayer, or creating a UIView and drawing it directly into the draw() method.
let easierPath = EasierPath(100,100)
.right(100)
.curve(to: .down(200), .bezier(.rightDown(50,50), .leftDown(25,150)))
.left(100)
.curve(to:.up(200), .bezier(.rightUp(25,50), .leftUp(50,150)))
let layer = easierPath.makeLayer(lineWidth:3,lineColor: .white,fillColor:.systemPink)
view.layer.addSublayer(layer)
Using the above EasierPath, you don't have to remember where it ended before, just specify which direction to draw the line.
Even if the starting point changes, you do not need to change each point because it is drawn in a direction based on that point.
You don't have to create a CAShapeLayer or UIView, but you can create a layer if you specify a style using the makeLayer method in EasierPath.
let shape = EasierPath(Shape, CGRect())
square | circle | rectangle | oval | triangle | rhombus |
---|---|---|---|---|---|
let easierPath = EasierPath(100,600)
.right(100)
.curve(to: .down(200), .bezier(.rightDown(50,50), .leftDown(25,150)))
.left(100)
.curve(to:.up(200), .bezier(.rightUp(25,50), .leftUp(50,150)))
let gradientLayer = easierPath.makeGradientLayer(startPoint: CGPoint(x: 0.0, y: 0.5), endPoint: CGPoint(x: 1.0, y: 0.5), gradientColors: [UIColor.blue.cgColor,UIColor.red.cgColor])
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 13.0
- Swift 5.0
EasierPath is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'EasierPath'
87.2%
Fomagran, [email protected]
EasierPath is available under the MIT license. See the LICENSE file for more info.