Created
March 6, 2023 05:49
-
-
Save iamchiwon/0944273684bed8f7819945d23b83a46e to your computer and use it in GitHub Desktop.
Chainable Bezierr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIBezierPath: Chainable {} | |
extension Chain where T: UIBezierPath { | |
func move(to point: CGPoint) -> Chain { | |
origin.move(to: point) | |
return self | |
} | |
func moveTo(x: CGFloat, y: CGFloat) -> Chain { | |
return move(to: CGPoint(x: x, y: y)) | |
} | |
func addLine(to point: CGPoint) -> Chain { | |
origin.addLine(to: point) | |
return self | |
} | |
func addLineTo(x: CGFloat, y: CGFloat) -> Chain { | |
return addLine(to: CGPoint(x: x, y: y)) | |
} | |
func addQuadCurve(to point: CGPoint, controlPoint: CGPoint) -> Chain { | |
origin.addQuadCurve(to: point, controlPoint: controlPoint) | |
return self | |
} | |
func addQuadCurve(x: CGFloat, y: CGFloat, controlX: CGFloat, controlY: CGFloat) -> Chain { | |
return addQuadCurve(to: CGPoint(x: x, y: y), | |
controlPoint: CGPoint(x: controlX, y: controlY)) | |
} | |
func addCurve(to point: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint) -> Chain { | |
origin.addCurve(to: point, controlPoint1: controlPoint1, controlPoint2: controlPoint2) | |
return self | |
} | |
func addArc(withCenter: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool = true) -> Chain { | |
origin.addArc(withCenter: withCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise) | |
return self | |
} | |
func addArc(centerX: CGFloat, centerY: CGFloat, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool = true) -> Chain { | |
return addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise) | |
} | |
func setColor(_ color: UIColor) -> Chain { | |
color.set() | |
return self | |
} | |
func setStrokeColor(_ color: UIColor) -> Chain { | |
color.setStroke() | |
return self | |
} | |
func setFillColor(_ color: UIColor) -> Chain { | |
color.setFill() | |
return self | |
} | |
func line(width: CGFloat, cap: CGLineCap = .round, joint: CGLineJoin = .round) -> Chain { | |
origin.lineWidth = width | |
origin.lineCapStyle = cap | |
origin.lineJoinStyle = joint | |
return self | |
} | |
@discardableResult | |
func close() -> Chain { | |
origin.close() | |
return self | |
} | |
@discardableResult | |
func stroke() -> Chain { | |
origin.stroke() | |
return self | |
} | |
@discardableResult | |
func fill() -> Chain { | |
origin.fill() | |
return self | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol Chainable { | |
} | |
class Chain<T> { | |
var origin: T | |
init(origin: T) { | |
self.origin = origin | |
} | |
} | |
extension Chainable { | |
var chain: Chain<Self> { | |
return Chain(origin: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment