Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SKTriangleView.swift
- // sport
- //
- // Created by Kviatkovskii on 26/03/2018.
- // Copyright © 2018 Kviatkovskii. All rights reserved.
- //
- import UIKit
- final class SKTriangleView: UIView {
- fileprivate let color: UIColor
- fileprivate let titleLabel: UILabel = {
- let label = UILabel()
- label.textAlignment = .center
- label.font = .systemFont(ofSize: 23)
- return label
- }()
- fileprivate func setConstraints() {
- titleLabel.snp.makeConstraints { (make) in
- make.bottom.equalToSuperview().inset(20)
- make.left.right.equalToSuperview().inset(25)
- make.height.equalTo(30)
- }
- }
- init(color: UIColor, title: String) {
- self.color = color
- titleLabel.text = title.uppercased()
- super.init(frame: CGRect())
- backgroundColor = .clear
- addSubview(titleLabel)
- setConstraints()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func draw(_ rect: CGRect) {
- let context = UIGraphicsGetCurrentContext()
- context!.setFillColor(UIColor.clear.cgColor)
- context!.fill(rect)
- let size = bounds.size
- // line
- let pLine1 = CGPoint(x: 0, y: size.height * 0.45)
- let pLine2 = CGPoint(x: size.width / 2, y: 1)
- let pLine3 = CGPoint(x: size.width, y: size.height * 0.45)
- let linePath = UIBezierPath()
- linePath.move(to: pLine1)
- linePath.addLine(to: pLine2)
- linePath.addLine(to: pLine3)
- linePath.miterLimit = 4
- linePath.usesEvenOddFillRule = true
- linePath.lineWidth = 2.0
- linePath.lineJoinStyle = .round
- linePath.lineCapStyle = .round
- context?.setStrokeColor(color.cgColor)
- linePath.stroke()
- // trianle
- let pTriangle1 = CGPoint(x: pLine1.x, y: pLine1.y + 5)
- let pTriangle2 = CGPoint(x: pLine2.x, y: pLine2.y + 5)
- let pTriangle3 = CGPoint(x: pLine3.x, y: pLine3.y + 5)
- let pTriangle4 = CGPoint(x: pLine3.x, y: size.height)
- let pTriangle5 = CGPoint(x: 0, y: size.height)
- let pTriangle6 = CGPoint(x: pTriangle1.x, y: pTriangle1.y)
- let trianglePath = UIBezierPath()
- trianglePath.move(to: pTriangle1)
- trianglePath.addLine(to: pTriangle2)
- trianglePath.addLine(to: pTriangle3)
- trianglePath.addLine(to: pTriangle4)
- trianglePath.addLine(to: pTriangle5)
- trianglePath.addLine(to: pTriangle6)
- context?.setFillColor(color.cgColor)
- trianglePath.fill()
- }
- }
Add Comment
Please, Sign In to add comment