Guest User

Untitled

a guest
Apr 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. //
  2. // SKTriangleView.swift
  3. // sport
  4. //
  5. // Created by Kviatkovskii on 26/03/2018.
  6. // Copyright © 2018 Kviatkovskii. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. final class SKTriangleView: UIView {
  12.  
  13. fileprivate let color: UIColor
  14.  
  15. fileprivate let titleLabel: UILabel = {
  16. let label = UILabel()
  17. label.textAlignment = .center
  18. label.font = .systemFont(ofSize: 23)
  19. return label
  20. }()
  21.  
  22. fileprivate func setConstraints() {
  23. titleLabel.snp.makeConstraints { (make) in
  24. make.bottom.equalToSuperview().inset(20)
  25. make.left.right.equalToSuperview().inset(25)
  26. make.height.equalTo(30)
  27. }
  28. }
  29.  
  30. init(color: UIColor, title: String) {
  31. self.color = color
  32. titleLabel.text = title.uppercased()
  33. super.init(frame: CGRect())
  34.  
  35. backgroundColor = .clear
  36. addSubview(titleLabel)
  37. setConstraints()
  38. }
  39.  
  40. required init?(coder aDecoder: NSCoder) {
  41. fatalError("init(coder:) has not been implemented")
  42. }
  43.  
  44. override func draw(_ rect: CGRect) {
  45. let context = UIGraphicsGetCurrentContext()
  46. context!.setFillColor(UIColor.clear.cgColor)
  47. context!.fill(rect)
  48.  
  49. let size = bounds.size
  50.  
  51. // line
  52. let pLine1 = CGPoint(x: 0, y: size.height * 0.45)
  53. let pLine2 = CGPoint(x: size.width / 2, y: 1)
  54. let pLine3 = CGPoint(x: size.width, y: size.height * 0.45)
  55.  
  56. let linePath = UIBezierPath()
  57. linePath.move(to: pLine1)
  58. linePath.addLine(to: pLine2)
  59. linePath.addLine(to: pLine3)
  60.  
  61. linePath.miterLimit = 4
  62. linePath.usesEvenOddFillRule = true
  63. linePath.lineWidth = 2.0
  64. linePath.lineJoinStyle = .round
  65. linePath.lineCapStyle = .round
  66. context?.setStrokeColor(color.cgColor)
  67. linePath.stroke()
  68.  
  69. // trianle
  70. let pTriangle1 = CGPoint(x: pLine1.x, y: pLine1.y + 5)
  71. let pTriangle2 = CGPoint(x: pLine2.x, y: pLine2.y + 5)
  72. let pTriangle3 = CGPoint(x: pLine3.x, y: pLine3.y + 5)
  73. let pTriangle4 = CGPoint(x: pLine3.x, y: size.height)
  74. let pTriangle5 = CGPoint(x: 0, y: size.height)
  75. let pTriangle6 = CGPoint(x: pTriangle1.x, y: pTriangle1.y)
  76.  
  77. let trianglePath = UIBezierPath()
  78. trianglePath.move(to: pTriangle1)
  79. trianglePath.addLine(to: pTriangle2)
  80. trianglePath.addLine(to: pTriangle3)
  81. trianglePath.addLine(to: pTriangle4)
  82. trianglePath.addLine(to: pTriangle5)
  83. trianglePath.addLine(to: pTriangle6)
  84.  
  85. context?.setFillColor(color.cgColor)
  86. trianglePath.fill()
  87. }
  88. }
Add Comment
Please, Sign In to add comment