Skip to content

Instantly share code, notes, and snippets.

@iamchiwon
Created March 6, 2023 05:49
Show Gist options
  • Save iamchiwon/0944273684bed8f7819945d23b83a46e to your computer and use it in GitHub Desktop.
Save iamchiwon/0944273684bed8f7819945d23b83a46e to your computer and use it in GitHub Desktop.
Chainable Bezierr
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
}
}
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)
}
}
import UIKit
class MyCloseButton: UIControl {
init() {
super.init(frame: CGRect.zero)
backgroundColor = .clear
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func draw(_ rect: CGRect) {
let width = rect.size.width
let height = rect.size.height
let width10 = width * 0.2
let lineWidth = width * 0.1
let backColor = isSelected ? UIColor.green : UIColor.orange
let outlineColor = isSelected ? UIColor.blue : UIColor.red
// ㅁ
UIBezierPath().chain
.moveTo(x: 0, y: 0)
.addLineTo(x: 0, y: height)
.addLineTo(x: width, y: height)
.addLineTo(x: width, y: 0)
.close()
.setColor(backColor)
.fill()
.setStrokeColor(outlineColor)
.line(width: lineWidth)
.stroke()
// \
UIBezierPath().chain
.moveTo(x: width10, y: width10)
.addLineTo(x: width - width10, y: height - width10)
.setStrokeColor(UIColor.black)
.line(width: lineWidth)
.stroke()
// /
UIBezierPath().chain
.moveTo(x: width - width10, y: width10)
.addLineTo(x: width10, y: height - width10)
.setStrokeColor(UIColor.black)
.line(width: lineWidth)
.stroke()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isSelected = true
setNeedsDisplay()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isSelected = false
setNeedsDisplay()
if let touch = touches.first,
bounds.contains(touch.location(in: self)) {
sendActions(for: .touchUpInside)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment