BeeReel/BeeReel/Class/Base/View/TabBar/BRTabBarItemContentView.swift
2025-07-31 19:26:12 +08:00

162 lines
4.3 KiB
Swift

//
// BRTabBarItemContentView.swift
// BeeReel
//
// Created by 鸿 on 2025/6/25.
//
import UIKit
class BRTabBarItemContentView: UIView {
///
open var selected = false
///
open var highlighted = false
///
open var highlightEnabled = true
var title: String? {
didSet {
// selectedView.title = title
}
}
var image: UIImage? {
didSet {
// normalView.image = image
updateLayout()
}
}
var selectedImage: UIImage? {
didSet {
// selectedView.image = selectedImage
updateLayout()
}
}
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
return imageView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = false
self.layer.masksToBounds = true
addSubview(imageView)
imageView.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.bottom.equalToSuperview().offset(-(UIScreen.tabbarSafeBottomMargin + 2))
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func updateDisplay() {
}
func updateLayout() {
imageView.image = selected ? selectedImage : image
}
}
extension BRTabBarItemContentView {
// MARK: - INTERNAL METHODS
internal final func select(animated: Bool, completion: (() -> ())?) {
selected = true
if highlightEnabled && highlighted {
highlighted = false
dehighlightAnimation(animated: animated, completion: { [weak self] in
self?.updateDisplay()
self?.selectAnimation(animated: animated, completion: completion)
})
} else {
updateDisplay()
selectAnimation(animated: animated, completion: completion)
}
}
internal final func deselect(animated: Bool, completion: (() -> ())?) {
selected = false
updateDisplay()
self.deselectAnimation(animated: animated, completion: completion)
}
internal final func reselect(animated: Bool, completion: (() -> ())?) {
if selected == false {
select(animated: animated, completion: completion)
} else {
if highlightEnabled && highlighted {
highlighted = false
dehighlightAnimation(animated: animated, completion: { [weak self] in
self?.reselectAnimation(animated: animated, completion: completion)
})
} else {
reselectAnimation(animated: animated, completion: completion)
}
}
}
internal final func highlight(animated: Bool, completion: (() -> ())?) {
if !highlightEnabled {
return
}
if highlighted == true {
return
}
highlighted = true
self.highlightAnimation(animated: animated, completion: completion)
}
internal final func dehighlight(animated: Bool, completion: (() -> ())?) {
if !highlightEnabled {
return
}
if !highlighted {
return
}
highlighted = false
self.dehighlightAnimation(animated: animated, completion: completion)
}
internal func badgeChanged(animated: Bool, completion: (() -> ())?) {
self.badgeChangedAnimation(animated: animated, completion: completion)
}
// MARK: - ANIMATION METHODS
func selectAnimation(animated: Bool, completion: (() -> ())?) {
self.imageView.image = self.selectedImage
}
func deselectAnimation(animated: Bool, completion: (() -> ())?) {
self.imageView.image = self.image
}
func reselectAnimation(animated: Bool, completion: (() -> ())?) {
completion?()
}
func highlightAnimation(animated: Bool, completion: (() -> ())?) {
completion?()
}
func dehighlightAnimation(animated: Bool, completion: (() -> ())?) {
completion?()
}
func badgeChangedAnimation(animated: Bool, completion: (() -> ())?) {
completion?()
}
}