Created
March 12, 2018 17:22
-
-
Save vialyx/76c1117944e33362c4fe0539f3c2d888 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//: 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