123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //
- // NCMainTabBar.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 06/01/2020.
- // Copyright © 2020 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import Foundation
- @IBDesignable class NCMainTabBar: UITabBar {
- override var traitCollection: UITraitCollection {
- return UITraitCollection(horizontalSizeClass: .compact)
- }
- override var backgroundColor: UIColor? {
- get {
- return self.fillColor
- }
- set {
- fillColor = newValue
- }
- }
-
- private var fillColor: UIColor!
- private var shapeLayer: CALayer?
- private func addShape() {
- let shapeLayer = CAShapeLayer()
- shapeLayer.path = createPath()
- shapeLayer.fillColor = backgroundColor?.cgColor
- if let oldShapeLayer = self.shapeLayer {
- self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
- } else {
- self.layer.insertSublayer(shapeLayer, at: 0)
- }
- self.shapeLayer = shapeLayer
- }
- override func layoutSubviews() {
- super.layoutSubviews()
- self.layer.shadowPath = createPath()
- self.layer.shadowRadius = 5
- self.layer.shadowOffset = .zero
- self.layer.shadowOpacity = 0.25
- }
- override func draw(_ rect: CGRect) {
- self.addShape()
- }
- func createPath() -> CGPath {
- let height: CGFloat = 28
- let margin: CGFloat = 8
- let path = UIBezierPath()
- let centerWidth = self.frame.width / 2
- path.move(to: CGPoint(x: 0, y: 0)) // start top left
- path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
- // first curve down
- 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)
- // complete the rect
- path.addLine(to: CGPoint(x: self.frame.width, y: 0))
- path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
- path.addLine(to: CGPoint(x: 0, y: self.frame.height))
- path.close()
- return path.cgPath
- }
- override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
- let button = self.viewWithTag(99)
- if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
- return true
- } else {
- return false
- }
- }
- func createPathCircle() -> CGPath {
- let radius: CGFloat = 37.0
- let path = UIBezierPath()
- let centerWidth = self.frame.width / 2
- path.move(to: CGPoint(x: 0, y: 0))
- path.addLine(to: CGPoint(x: (centerWidth - radius * 2), y: 0))
- path.addArc(withCenter: CGPoint(x: centerWidth, y: 0), radius: radius, startAngle: CGFloat(180).degreesToRadians, endAngle: CGFloat(0).degreesToRadians, clockwise: false)
- path.addLine(to: CGPoint(x: self.frame.width, y: 0))
- path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
- path.addLine(to: CGPoint(x: 0, y: self.frame.height))
- path.close()
- return path.cgPath
- }
- }
- extension CGFloat {
- var degreesToRadians: CGFloat { return self * .pi / 180 }
- var radiansToDegrees: CGFloat { return self * 180 / .pi }
- }
|