随笔 - 399,  文章 - 0,  评论 - 7,  阅读 - 21万

 

 

配置基本属性的枚举

复制代码
/// 根控制器tabBar配置
enum JYAppRootConfigEnum: String {
    
    case centerItem = "首页"
    case expandGuestItem = "拓客"
    case mineItem = "我的"
    
    /// 正常图片名称
    var normalImageStr: String {
        switch self {
        case .centerItem:
            return "tab_home_unSelected"
        case .expandGuestItem:
            return "tab_match_unSelected"
        case .mineItem:
            return "tab_mine_unSelected"
        }
    }
    /// 高亮时图标名称
    var selectImageStr: String {
        switch self {
        case .centerItem:
            return "tab_home_selected"
        case .expandGuestItem:
            return "tab_match_selected"
        case .mineItem:
            return "tab_mine_selected"
        }
    }
}
复制代码

 

 

1.自定义代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import UIKit
 
/**
 *  自定义tabBar
 */
 
class JYCustomerTabbarView: UITabBar {
    /// 点击中心按钮
    var clickCenterBlock:(() -> Void)?
    /// 当前选中标记
    private var currentIndex: Int = 0
    /// 中心按钮
    private lazy var centerButton: UIButton = {
        let button = UIButton(type: .custom)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setImage(UIImage(named: JYAppRootConfigEnum.expandGuestItem.normalImageStr), for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.addTarget(self, action: #selector(centerButtonClick(sender:)), for: .touchUpInside)
        return button
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
//MARK: ----交互
extension JYCustomerTabbarView{
    /// 设置选中项,更新中心按钮状态
    ///
    /// - Parameter index: 当前选中标记
    func setSelectIndex(_ index: Int){
        if currentIndex != index{
            let isSelcted  = (index == 1) ? true : false
            centerButton.isSelected = isSelcted
            if index == 1{
                //选中扩客
                let type = JYAppRootConfigEnum.expandGuestItem
                let animate = makeScaleAimation(fromValue: 1, toValue: 0.4, duration: 0.2)
                animate.fillMode = .forwards
                animate.isRemovedOnCompletion = false
                animate.delegate = self
                centerButton.layer.add(animate, forKey: "centerButtonSelected")
                self.selectedItem?.image = UIImage(named: type.selectImageStr)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                self.selectedItem?.title = type.rawValue
            }else if currentIndex == 1{
                //取消选中扩客
                guard self.items?.count ?? 0 > 2, let item = self.items?[1] else{
                    return
                }
                item.image = nil
                item.title = nil
                centerButton.isHidden = false
                let animate = makeScaleAimation(fromValue: 0.1, toValue: 1.0, duration: 0.2)
                animate.fillMode = .forwards
                animate.isRemovedOnCompletion = false
                animate.delegate = self
                self.centerButton.layer.add(animate, forKey: "centerButtonCancle")
            }
            currentIndex = index
        }
    }
    /// centerButton点击事件
    @objc private func centerButtonClick(sender: UIButton) {
        clickCenterBlock?()
        self.setSelectIndex(1)
    }
    // 处理超出点击区域的响应事件问题
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let v = super.hitTest(point, with: event)
        if v == nil , self.isHidden == false {
            let tempPoint = centerButton.convert(point, from: self)
            if currentIndex != 1, self.centerButton.bounds.contains(tempPoint) {
                return centerButton
            }
        }
        return v
    }
}
//MARK: ----动画
extension JYCustomerTabbarView: CAAnimationDelegate{
    //动画结束
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        //中间按钮选中
        if centerButton.layer.animation(forKey: "centerButtonSelected") == anim{
            centerButton.isHidden = true
            centerButton.layer.removeAllAnimations()
            if let imageView = getCenterTabBarItemImageView(){
                let animation = makeScaleAimation(fromValue: 0.5, toValue: 1.2, duration: 0.25)
                imageView.layer.add(animation, forKey: nil)
            }
        }else if self.centerButton.layer.animation(forKey: "centerButtonCancle") == anim {
            //中间按钮取消选中
            centerButton.layer.removeAllAnimations()
            let animation = makeScaleAimation(fromValue: 1, toValue: 1.2, duration: 0.1)
            self.centerButton.layer.add(animation, forKey: nil)
        }
    }
    /// 创建缩放动画
    ///
    /// - Parameters:
    ///   - fromValue: 起始缩放比例
    ///   - toValue: 结束缩放比例
    ///   - duration: 时长
    /// - Returns: 动画
    func makeScaleAimation(fromValue: Double, toValue: Double, duration: Double) -> CABasicAnimation{
        let animation = CABasicAnimation(keyPath: "transform.scale")
        animation.fromValue = fromValue
        animation.toValue = toValue
        animation.duration = duration
        return animation
    }
}
//MARK: ----UI
extension JYCustomerTabbarView{
    /// 获取中间TabBarItemImageView
    private func getCenterTabBarItemImageView() -> UIImageView?{
        if self.items?.count ?? 0 > 2, let item = self.items?[1], let tabBarButton = item.value(forKey: "view"), let barButton = tabBarButton as? UIControl, let imageView = barButton.value(forKey: "info") as? UIImageView{
            return imageView
        }
        return nil
    }
    /// 设置页面
    private func configUI(){
        self.isTranslucent = false
        self.tintColor = UIColor(hexString: "FF9E3E")
        self.setCernterButton()
    }
    /// 设置中间按钮
    private func setCernterButton() {
        self.addSubview(centerButton)
        let vd: [String: UIView] = ["centerButton": centerButton]
        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "[centerButton(52)]", options: [], metrics: nil, views: vd))
        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[centerButton(52)]", options: [], metrics: nil, views: vd))
        centerButton.topAnchor.constraint(equalTo: self.topAnchor, constant: -5).isActive = true
        centerButton.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0).isActive = true
        centerButton.layer.zPosition = 100
    }
}

  

 

2.使用代码

复制代码
     /// 自定义tabbar
        private let customerBar: JYCustomerTabbarView = JYCustomerTabbarView()    



/MARK: ---事件
extension JYAppRootViewController: UITabBarControllerDelegate{
    /// 切换控制器
    ///
    /// - Parameters:
    ///   - tabBarController: tabBarController
    ///   - viewController: 子控制器
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if let vcArr = self.viewControllers,
            let index = vcArr.firstIndex(of: viewController){
            DDLOG(message: "选中了tabBarController  == \(index)")
            //            customerBar.setSelectIndex(index)
        }
    }

    /// 切换到中间按钮控制器
    private func changeToCenterController(){
        self.selectedIndex = 1
    }

}

 //MARK: ---布局UI界面
extension JYAppRootViewController {
    /// 页面设置
    private func configUI(){
        configViewControllers()
        self.delegate = self
        self.selectedIndex = 0
        self.tabBar.tintColor = UIColor(hexString: "FF9E3E")
        
        //        self.setValue(customerBar, forKey: "tabBar")
        //        customerBar.setSelectIndex(0)
        //        customerBar.clickCenterBlock = {[weak self] in
        //            self?.changeToCenterController()
        //        }
    }
    /// 布局自控制器
    private func configViewControllers() {
        self.configController(vc: JYHomeCenterController(), itemEnum: .centerItem)
        self.configController(vc: JYMatchShopController(), itemEnum: .expandGuestItem)
        self.configController(vc: JYMyCenterController(), itemEnum: .mineItem)
    }
    /// 配置item Controller
    private func configController(vc controller: UIViewController , itemEnum: JYAppRootConfigEnum?) {
        if let  type = itemEnum {
            let title = type.rawValue
            controller.tabBarItem.title = title
            controller.tabBarItem.image = UIImage(named: type.normalImageStr)
            controller.tabBarItem.selectedImage = UIImage(named: type.selectImageStr)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            let nav = JYBaseNavController.init(rootViewController: controller)
            self.addChild(nav)
        }else {
            controller.tabBarItem.title = ""
            controller.tabBarItem.image = nil
            controller.tabBarItem.selectedImage = nil
            let nav = JYBaseNavController.init(rootViewController: controller)
            self.addChild(nav)
        }
    }
}
复制代码

 

posted on   懂事长qingzZ  阅读(670)  评论(4编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示