I’m workin on an iOS app which require a curve under UITabBarItem
as showing
I followed some tutorials and I capable to reach this code which make my UITabBar
curved,
import Foundation
import UIKit
class AppTabBar: UITabBar {
var curvePos : CGFloat = 0
private var shapeLayer: CALayer?
override func draw(_ rect: CGRect) {
curvePos = rect.width / 2
self.addShape(rect: rect)
}
private func addShape(rect: CGRect) {
let shapeLayer = CAShapeLayer()
shapeLayer.path = createPath(in: rect)
shapeLayer.strokeColor = UIColor.lightGray.cgColor
shapeLayer.fillColor = #colorLiteral(red: 0.9782002568, green: 0.9782230258, blue: 0.9782107472, alpha: 1)
shapeLayer.lineWidth = 0.5
shapeLayer.shadowOffset = CGSize(width:0, height:0)
shapeLayer.shadowRadius = 10
shapeLayer.shadowColor = UIColor.gray.cgColor
shapeLayer.shadowOpacity = 0.3
if let oldShapeLayer = self.shapeLayer {
self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
} else {
self.layer.insertSublayer(shapeLayer, at: 0)
}
self.shapeLayer = shapeLayer
}
func createPath(in rect: CGRect) -> CGPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 0, y: rect.height))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
path.addLine(to: CGPoint(x: rect.width, y: 0))
// adding Curve...
path.move(to: CGPoint(x: curvePos + 40, y: 0))
path.addQuadCurve(to: CGPoint(x: curvePos - 40, y: 0), controlPoint: CGPoint(x: curvePos, y: 70))
return path.cgPath
}
}
what I need now:
1-How can I change curvePos
value when tapping each item of UITabBar
to make curve under selected Item.
2- How can I animating UITabBarItem
or UITabBarItem image to make it as showing in above image.
Any help will be appreciated.
2
Answers
After many trying I could get what I would like to do: Bellow modified code of my AppTabBar:
and creating another class for UITabBarController as bellow:
set
CustomTabBarController
forTabBarController
andAppTabBar
forUITabBar
enter image description here