Skip to content

Instantly share code, notes, and snippets.

@ftp27
Created December 28, 2017 07:27
Show Gist options
  • Save ftp27/373214edbb998afd63eb4fc324f7e0b4 to your computer and use it in GitHub Desktop.
Save ftp27/373214edbb998afd63eb4fc324f7e0b4 to your computer and use it in GitHub Desktop.
import UIKit
class RepeatButton: UIButton {
fileprivate let lineTop:CAShapeLayer = {
let layer = CAShapeLayer()
let linePath = UIBezierPath()
//25x25
linePath.move(to: CGPoint(x: 10.5, y: 2.5))
linePath.addLine(to: CGPoint(x: 22.5, y: 2.5))
linePath.addLine(to: CGPoint(x: 22.5, y: 22.5))
linePath.addLine(to: CGPoint(x: 19, y: 22.5))
layer.path = linePath.cgPath
layer.fillColor = nil
layer.lineWidth = 2
layer.opacity = 1.0
layer.cornerRadius = 4
return layer
}()
fileprivate let lineBottom:CAShapeLayer = {
let layer = CAShapeLayer()
let linePath = UIBezierPath()
//25x25
linePath.move(to: CGPoint(x: 7, y: 2.5))
linePath.addLine(to: CGPoint(x: 2.5, y: 2.5))
linePath.addLine(to: CGPoint(x: 2.5, y: 22.5))
linePath.addLine(to: CGPoint(x: 13.5, y: 22.5))
linePath.lineJoinStyle = .bevel
linePath.lineCapStyle = .round
layer.path = linePath.cgPath
layer.fillColor = nil
layer.lineWidth = 2
layer.opacity = 1.0
return layer
}()
fileprivate let triangleTop: CAShapeLayer = {
let layer = CAShapeLayer()
let linePath = UIBezierPath()
//25x25
linePath.move(to: CGPoint(x: 13, y: 0))
linePath.addLine(to: CGPoint(x: 13, y: 5))
linePath.addLine(to: CGPoint(x: 8, y: 2.5))
layer.path = linePath.cgPath
layer.lineWidth = 0
layer.opacity = 1.0
return layer
}()
fileprivate let triangleBottom: CAShapeLayer = {
let layer = CAShapeLayer()
let linePath = UIBezierPath()
//25x25
linePath.move(to: CGPoint(x: 13, y: 20))
linePath.addLine(to: CGPoint(x: 18, y: 22.5))
linePath.addLine(to: CGPoint(x: 13, y: 25))
layer.path = linePath.cgPath
layer.lineWidth = 0
layer.opacity = 1.0
return layer
}()
fileprivate var lines: [CAShapeLayer] {
return [lineTop, lineBottom]
}
fileprivate var triangles: [CAShapeLayer] {
return [triangleTop, triangleBottom]
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
(lines + triangles).forEach {
self.layer.addSublayer($0)
}
}
override func draw(_ rect: CGRect) {
super.draw(rect)
let lineColor = tintColor.cgColor
(lines + triangles).forEach {
$0.transform = CATransform3DMakeScale(rect.width/25, rect.height/25, 1)
}
lines.forEach { $0.strokeColor = lineColor }
triangles.forEach { $0.fillColor = lineColor }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment