【发布时间】:2021-05-06 18:00:52
【问题描述】:
我正在开发一个 iOS 应用程序,该应用程序需要在 UITabBarItem 下显示一条曲线,如显示
我遵循了一些教程,并且能够访问使我的 UITabBar 弯曲的代码,
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
}
}
我现在需要的:
1-如何在点击UITabBar 的每个项目以在所选项目下制作曲线时更改curvePos 值。
2- 我如何为UITabBarItem 或 UITabBarItem 图像设置动画以使其如上图所示。
任何帮助将不胜感激。
【问题讨论】:
标签: ios xcode storyboard swift5 tabbar