// // MainScanPlusButton.swift // suixinkan // import CYLTabBarController import UIKit @MainActor @objcMembers /// 主 TabBar 中间扫码核销按钮,基于 CYLTabBarController 的 PlusButton 机制实现。 final class MainScanPlusButton: CYLPlusButton, CYLPlusButtonSubclassing { /// 点击扫码按钮时的回调,由 `MainTabBarController` 注入。 static var onScanTap: (() -> Void)? /// 构建中间扫码按钮实例,供 CYLTabBarController 注册使用。 static func plusButton() -> Any { let button = MainScanPlusButton() button.setImage(UIImage(named: "icon_scan"), for: .normal) button.titleLabel?.textAlignment = .center button.titleLabel?.font = UIFont.systemFont(ofSize: 10) button.setTitle("发布", for: .normal) button.setTitleColor(UIColor.gray, for: .normal) button.setTitle("选中", for: .selected) button.setTitleColor(UIColor(red:255.0/255.0,green:102.0/255.0,blue:0,alpha:1.0), for: .selected) button.adjustsImageWhenHighlighted = false button.sizeToFit() return button } /// 指定扫码按钮在 TabBar 中的视觉槽位(位于订单与数据之间)。 static func indexOfPlusButtonInTabBar() -> UInt { 2 } /// 调整扫码按钮垂直位置,使其略微凸出 TabBar 顶部。 static func constantOfPlusButtonCenterYOffset(forTabBarHeight tabBarHeight: CGFloat) -> CGFloat { -6 } /// 配置圆形背景与扫码图标,对齐参考工程 CustomMainTabBarCenterAction。 private func configureAppearance() { backgroundColor = .clear let circleSize: CGFloat = 58 let circleView = UIView() circleView.isUserInteractionEnabled = false circleView.backgroundColor = AppDesign.primary circleView.layer.cornerRadius = circleSize / 2 addSubview(circleView) let symbolConfig = UIImage.SymbolConfiguration(pointSize: 30, weight: .bold) let iconView = UIImageView( image: UIImage(named: "icon_scan") ) iconView.tintColor = .white iconView.contentMode = .scaleAspectFit iconView.isUserInteractionEnabled = false addSubview(iconView) circleView.translatesAutoresizingMaskIntoConstraints = false iconView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ circleView.centerXAnchor.constraint(equalTo: centerXAnchor), circleView.centerYAnchor.constraint(equalTo: centerYAnchor), circleView.widthAnchor.constraint(equalToConstant: circleSize), circleView.heightAnchor.constraint(equalToConstant: circleSize), iconView.centerXAnchor.constraint(equalTo: circleView.centerXAnchor), iconView.centerYAnchor.constraint(equalTo: circleView.centerYAnchor), iconView.widthAnchor.constraint(equalToConstant: 30), iconView.heightAnchor.constraint(equalToConstant: 30) ]) accessibilityLabel = "扫码核销" accessibilityIdentifier = "main.scan" } /// 响应用户点击,触发全局扫码流程。 @objc private func handleScanTap() { Self.onScanTap?() } }