85 lines
2.8 KiB
Swift
85 lines
2.8 KiB
Swift
//
|
|
// MPPositive_CustomTabBarView.swift
|
|
// MusicPlayer
|
|
//
|
|
// Created by Mr.Zhou on 2024/4/19.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class MPPositive_CustomTabBarView: UIView {
|
|
///背景图片
|
|
private lazy var bgImageView:UIImageView = {
|
|
let bgImageView:UIImageView = .init(image: .init(named: "TabBar'bg"))
|
|
bgImageView.contentMode = .scaleAspectFill
|
|
bgImageView.layer.masksToBounds = true
|
|
bgImageView.layer.cornerRadius = 24*width
|
|
bgImageView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
|
|
return bgImageView
|
|
}()
|
|
///items
|
|
private lazy var tabBarItems:[MPPositive_CustomTabBarItem] = {
|
|
var tabBarItems:[MPPositive_CustomTabBarItem] = []
|
|
for index in 0..<titles.count {
|
|
let itemView:MPPositive_CustomTabBarItem = .init(frame: .init(x: 0, y: 0, width: 36*width, height: 36*width))
|
|
itemView.tag = index
|
|
itemView.title = titles[index]
|
|
itemView.isUserInteractionEnabled = true
|
|
itemView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(switchActionClick(_ :))))
|
|
tabBarItems.append(itemView)
|
|
}
|
|
return tabBarItems
|
|
}()
|
|
///titles
|
|
private lazy var titles:[String] = ["B_Home","B_Seach","B_Mine"]
|
|
///当前选中itemIndex
|
|
private lazy var selectedItemIndex:Int = 0
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
confirgue()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
}
|
|
//配置
|
|
private func confirgue() {
|
|
backgroundColor = .clear
|
|
//超出部分不切除
|
|
layer.masksToBounds = false
|
|
//添加背景
|
|
addSubview(bgImageView)
|
|
bgImageView.snp.makeConstraints { make in
|
|
make.left.top.right.bottom.equalToSuperview()
|
|
}
|
|
//添加ItemsView
|
|
tabBarItems.forEach { item in
|
|
addSubview(item)
|
|
item.snp.makeConstraints { make in
|
|
make.centerY.equalToSuperview()
|
|
make.width.height.equalTo(36*width)
|
|
make.centerX.equalToSuperview().multipliedBy(0.45 + 0.55*Double(item.tag))
|
|
}
|
|
}
|
|
//默认首位item处于选中状态
|
|
tabBarItems[0].isSelected = true
|
|
}
|
|
|
|
//点击事件
|
|
@objc private func switchActionClick(_ sender:UITapGestureRecognizer) {
|
|
endEditing(true)
|
|
let tag = sender.view?.tag ?? 0
|
|
//根据tag值传送
|
|
guard selectedItemIndex != tag else {
|
|
//无效点击
|
|
return
|
|
}
|
|
tabBarItems.forEach { item in
|
|
item.isSelected = item.tag == tag
|
|
}
|
|
selectedItemIndex = tag
|
|
//发送页面切换通知
|
|
NotificationCenter.notificationKey.post(notificationName: .switch_tabBarItem, object: tag)
|
|
}
|
|
}
|