NCMainTabBar.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // NCMainTabBar.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/01/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. @IBDesignable class NCMainTabBar: UITabBar {
  25. override var traitCollection: UITraitCollection {
  26. return UITraitCollection(horizontalSizeClass: .compact)
  27. }
  28. override var backgroundColor: UIColor? {
  29. get {
  30. return self.fillColor
  31. }
  32. set {
  33. fillColor = newValue
  34. self.setNeedsDisplay()
  35. }
  36. }
  37. private var fillColor: UIColor!
  38. private var shapeLayer: CALayer?
  39. private func addShape() {
  40. let shapeLayer = CAShapeLayer()
  41. shapeLayer.path = createPath()
  42. shapeLayer.fillColor = backgroundColor?.cgColor
  43. if let oldShapeLayer = self.shapeLayer {
  44. self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
  45. } else {
  46. self.layer.insertSublayer(shapeLayer, at: 0)
  47. }
  48. self.shapeLayer = shapeLayer
  49. }
  50. override func layoutSubviews() {
  51. super.layoutSubviews()
  52. self.layer.shadowPath = createPath()
  53. self.layer.shadowRadius = 5
  54. self.layer.shadowOffset = .zero
  55. self.layer.shadowOpacity = 0.25
  56. }
  57. override func draw(_ rect: CGRect) {
  58. self.addShape()
  59. }
  60. func createPath() -> CGPath {
  61. let height: CGFloat = 28
  62. let margin: CGFloat = 8
  63. let path = UIBezierPath()
  64. let centerWidth = self.frame.width / 2
  65. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  66. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  67. // first curve down
  68. path.addArc(withCenter: CGPoint(x: centerWidth, y: 0), radius: height + margin, startAngle: CGFloat(180 * Double.pi / 180), endAngle: CGFloat(0 * Double.pi / 180), clockwise: false)
  69. // complete the rect
  70. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  71. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  72. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  73. path.close()
  74. return path.cgPath
  75. }
  76. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  77. let button = self.viewWithTag(99)
  78. if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
  79. return true
  80. } else {
  81. return false
  82. }
  83. }
  84. func createPathCircle() -> CGPath {
  85. let radius: CGFloat = 37.0
  86. let path = UIBezierPath()
  87. let centerWidth = self.frame.width / 2
  88. path.move(to: CGPoint(x: 0, y: 0))
  89. path.addLine(to: CGPoint(x: (centerWidth - radius * 2), y: 0))
  90. path.addArc(withCenter: CGPoint(x: centerWidth, y: 0), radius: radius, startAngle: CGFloat(180).degreesToRadians, endAngle: CGFloat(0).degreesToRadians, clockwise: false)
  91. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  92. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  93. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  94. path.close()
  95. return path.cgPath
  96. }
  97. }
  98. extension CGFloat {
  99. var degreesToRadians: CGFloat { return self * .pi / 180 }
  100. var radiansToDegrees: CGFloat { return self * 180 / .pi }
  101. }