Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created March 12, 2018 17:22
Show Gist options
  • Save vialyx/76c1117944e33362c4fe0539f3c2d888 to your computer and use it in GitHub Desktop.
Save vialyx/76c1117944e33362c4fe0539f3c2d888 to your computer and use it in GitHub Desktop.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class HexagonView: UIView {
private let borderLineWidth: CGFloat = 4
var borderLineColor: UIColor = .clear
override func draw(_ rect: CGRect) {
let maskLayer = CAShapeLayer()
maskLayer.fillRule = kCAFillRuleEvenOdd
maskLayer.frame = bounds
let path = maskPath
guard let context = UIGraphicsGetCurrentContext() else { return }
context.setStrokeColor(borderLineColor.cgColor)
context.setLineWidth(borderLineWidth)
context.addPath(path.cgPath)
context.strokePath()
maskLayer.path = path.cgPath
layer.mask = maskLayer
}
var maskPath: UIBezierPath {
let width = frame.size.width
let height = frame.size.height
let hPadding = width * 1 / 8 / 2
let path = UIBezierPath()
path.move(to: CGPoint(x: width / 2, y: 0))
path.addLine(to: CGPoint(x: width - hPadding, y: height / 4))
path.addLine(to: CGPoint(x: width - hPadding, y: height * 3 / 4))
path.addLine(to: CGPoint(x: width / 2, y: height))
path.addLine(to: CGPoint(x: hPadding, y: height * 3 / 4))
path.addLine(to: CGPoint(x: hPadding, y: height / 4))
path.close()
return path
}
}
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let hexagon = HexagonView()
hexagon.frame = CGRect(x: 150, y: 200, width: 200, height: 200)
hexagon.borderLineColor = UIColor.red
view.addSubview(hexagon)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment