Skip to content

Instantly share code, notes, and snippets.

@phuongddx
Created July 4, 2020 08:16
Show Gist options
  • Save phuongddx/b9efbcabc754c9372f1c16db62ff036d to your computer and use it in GitHub Desktop.
Save phuongddx/b9efbcabc754c9372f1c16db62ff036d to your computer and use it in GitHub Desktop.
//
// HomeTabbar.swift
// Alamofire
//
// Created by DoanDuyPhuong on 6/29/20.
//
import Foundation
import UIKit
@IBDesignable
class HomeTabBar: UITabBar {
let curveTop: CGFloat = 40
var curveRadius: CGFloat {
let height = self.frame.height - self.getSafeBottom()
return (height / 2) + curveTop
// return 42
}
var shapLayer: CAShapeLayer?
var circleLayer: CAShapeLayer?
var circleYellowRadius: CGFloat {
// return self.curveRadius
let height = self.frame.height - self.getSafeBottom()
// return (height / 2) + curveTop
return 70
}
override func draw(_ rect: CGRect) {
self.addShape()
self.addYellowCicle()
}
private func addShape() {
let shape = CAShapeLayer()
shape.path = self.createPath()
shape.strokeColor = UIColor.red.cgColor
shape.fillColor = UIColor.yellow.cgColor
shape.lineWidth = 1.0
if let s = self.shapLayer {
self.layer.replaceSublayer(s, with: shape)
}
else {
self.layer.insertSublayer(shape, at: 0)
}
self.shapLayer = shape
}
private func createPath() -> CGPath {
// https://stackoverflow.com/questions/22212440/using-math-class-to-convert-cosine-into-degrees?fbclid=IwAR0U6sSRByN_sV0EeQt0Q5I3HLOekxPiqe-iKv6eNjRBpuYjc8U68_IWZDg
// cos(x) = angle
// -> x =
let angle = (self.curveRadius - self.curveTop) / self.curveRadius
let angleRadian = acos(angle)
let engleToDegree = angleRadian * 180 / .pi
let startAngleDree = 180 + 90 - engleToDegree
let endAngleDree = 180 + 90 + engleToDegree
// cạnh đáy của tam giác cân ~ pitago
let bc = pow(self.curveRadius, 2)
let hc = bc - pow(self.curveRadius - self.curveTop, 2)
let ac = sqrt(hc)
let path = UIBezierPath()
let centerWidth = self.frame.width / 2
path.move(to: .zero)
path.addLine(to: .init(x: centerWidth - ac, y: 0))
path.move(to: .init(x: centerWidth - ac, y: 0))
path.addArc(withCenter: self.getCenter(), radius: self.curveRadius, startAngle: .pi * startAngleDree / 180.0, endAngle: .pi * endAngleDree / 180, clockwise: true)
path.move(to: .init(x: centerWidth + ac, y: 0))
path.addLine(to: CGPoint.init(x: self.frame.width, y: 0))
path.close()
return path.cgPath
}
private func getCenter() -> CGPoint {
return CGPoint.init(x: self.frame.width / 2, y: (self.frame.height - self.getSafeBottom() ) / 2 )
}
private func getCenterCicle() -> CGPoint {
return CGPoint.init(x: self.frame.width / 2, y: (self.frame.height / 4 - self.getSafeBottom() ) / 2 )
}
//
private func addYellowCicle() {
let radius = self.circleYellowRadius
let shape = CAShapeLayer()
shape.fillColor = UIColor.red.cgColor
shape.path = UIBezierPath.init(arcCenter: self.getCenterCicle(), radius: radius / 2, startAngle: 0, endAngle: .pi * 2, clockwise: true).cgPath
if let s = self.circleLayer {
self.layer.replaceSublayer(s, with: shape)
} else {
self.layer.insertSublayer(shape, at: 1)
}
self.circleLayer = shape
}
private func getSafeBottom() -> CGFloat {
if #available(iOS 11.0, *) {
return self.safeAreaInsets.bottom
} else {
return 0
}
}
}
extension HomeTabBar {
override var traitCollection: UITraitCollection {
return UITraitCollection.init(horizontalSizeClass: .unspecified)
}
override func layoutSubviews() {
super.layoutSubviews()
self.itemPositioning = .centered
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment