NCMainTabBar.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. private var shapeLayer: CALayer?
  29. private func addShape() {
  30. let shapeLayer = CAShapeLayer()
  31. shapeLayer.path = createPath()
  32. shapeLayer.fillColor = NCBrandColor.sharedInstance.tabBar.cgColor
  33. if let oldShapeLayer = self.shapeLayer {
  34. self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
  35. } else {
  36. self.layer.insertSublayer(shapeLayer, at: 0)
  37. }
  38. self.shapeLayer = shapeLayer
  39. }
  40. override func layoutSubviews() {
  41. super.layoutSubviews()
  42. self.layer.shadowPath = createPath()
  43. self.layer.shadowRadius = 5
  44. self.layer.shadowOffset = .zero
  45. self.layer.shadowOpacity = 0.25
  46. }
  47. override func draw(_ rect: CGRect) {
  48. self.addShape()
  49. }
  50. func createPath() -> CGPath {
  51. let height: CGFloat = 28
  52. let margin: CGFloat = 8
  53. let path = UIBezierPath()
  54. let centerWidth = self.frame.width / 2
  55. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  56. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  57. // first curve down
  58. 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)
  59. // complete the rect
  60. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  61. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  62. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  63. path.close()
  64. return path.cgPath
  65. }
  66. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  67. let button = self.viewWithTag(99)
  68. if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
  69. return true
  70. } else {
  71. return false
  72. }
  73. }
  74. func createPathCircle() -> CGPath {
  75. let radius: CGFloat = 37.0
  76. let path = UIBezierPath()
  77. let centerWidth = self.frame.width / 2
  78. path.move(to: CGPoint(x: 0, y: 0))
  79. path.addLine(to: CGPoint(x: (centerWidth - radius * 2), y: 0))
  80. path.addArc(withCenter: CGPoint(x: centerWidth, y: 0), radius: radius, startAngle: CGFloat(180).degreesToRadians, endAngle: CGFloat(0).degreesToRadians, clockwise: false)
  81. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  82. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  83. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  84. path.close()
  85. return path.cgPath
  86. }
  87. }
  88. extension CGFloat {
  89. var degreesToRadians: CGFloat { return self * .pi / 180 }
  90. var radiansToDegrees: CGFloat { return self * 180 / .pi }
  91. }