187 lines
5.8 KiB
Mathematica
187 lines
5.8 KiB
Mathematica
|
|
//
|
||
|
|
// QXTabBar.m
|
||
|
|
// FreeTabbar_JoeJin
|
||
|
|
//
|
||
|
|
// Created by J-Mac on 2024/5/24.
|
||
|
|
// Copyright © 2024 JoeJin-QQ:853105953. All rights reserved.
|
||
|
|
//
|
||
|
|
|
||
|
|
#import "QXTabBar.h"
|
||
|
|
#import <Masonry/Masonry.h>
|
||
|
|
#import "QXTabbarConfig.h"
|
||
|
|
#import "QXTabBarButton.h"
|
||
|
|
|
||
|
|
@interface QXTabBar()<QXTabBarButtonDelegate>
|
||
|
|
/// 当前选中的按钮
|
||
|
|
@property (nonatomic, weak) QXTabBarButton *selectedButton;
|
||
|
|
/// UITabBarItem对象数组
|
||
|
|
@property(nonatomic, strong)NSMutableArray *tabbarBtnArray;
|
||
|
|
/// norImage
|
||
|
|
@property (nonatomic, strong) NSMutableArray *norImageArrM;
|
||
|
|
/// SelImage
|
||
|
|
@property (nonatomic, strong) NSMutableArray *selImageArrM;
|
||
|
|
/// 背景图片
|
||
|
|
@property (nonatomic, strong) UIImageView *bgImageView;
|
||
|
|
@property (nonatomic, assign) BOOL centerCustom;
|
||
|
|
|
||
|
|
@property (nonatomic, weak) QXTabBarButton *messageBtn;
|
||
|
|
@end
|
||
|
|
|
||
|
|
@implementation QXTabBar
|
||
|
|
|
||
|
|
- (NSMutableArray *)tabbarBtnArray {
|
||
|
|
if (!_tabbarBtnArray) {
|
||
|
|
_tabbarBtnArray = [NSMutableArray array];
|
||
|
|
}
|
||
|
|
return _tabbarBtnArray;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (NSMutableArray *)norImageArrM {
|
||
|
|
if (!_norImageArrM) {
|
||
|
|
_norImageArrM = [NSMutableArray array];
|
||
|
|
}
|
||
|
|
return _norImageArrM;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (NSMutableArray *)selImageArrM {
|
||
|
|
if (!_selImageArrM) {
|
||
|
|
_selImageArrM = [NSMutableArray array];
|
||
|
|
}
|
||
|
|
return _selImageArrM;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (UIImageView *)bgImageView {
|
||
|
|
if (!_bgImageView) {
|
||
|
|
_bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabbar背景"]];
|
||
|
|
_bgImageView.userInteractionEnabled = YES;
|
||
|
|
}
|
||
|
|
return _bgImageView;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (instancetype)initWithFrame:(CGRect)frame centerCustom:(BOOL)centerCustom config:(QXTabbarConfig *)config{
|
||
|
|
self = [super initWithFrame:frame];
|
||
|
|
if (self) {
|
||
|
|
self.centerCustom = self.centerCustom;
|
||
|
|
[self addSubview:self.bgImageView];
|
||
|
|
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
|
make.top.equalTo(self).offset(0);//[[QXTabbarConfig shareInstance]bgImageOffset]
|
||
|
|
make.left.right.equalTo(self);
|
||
|
|
make.bottom.equalTo(self);
|
||
|
|
}];
|
||
|
|
}
|
||
|
|
return self;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
- (void)addTabBarButtonNorImageUrl:(NSString *)norImageUrl
|
||
|
|
selImageUrl:(NSString *)selImageUrl
|
||
|
|
title:(NSString *)title {
|
||
|
|
|
||
|
|
// 1.创建按钮
|
||
|
|
QXTabBarButton *tabBarBtn = [[QXTabBarButton alloc] init];
|
||
|
|
tabBarBtn.delegate = self;
|
||
|
|
[self addSubview:tabBarBtn];
|
||
|
|
// 2.设置数据
|
||
|
|
[tabBarBtn setTabBarImageUrl:norImageUrl selectedImg:selImageUrl title:title];
|
||
|
|
// 3.监听按钮点击
|
||
|
|
[tabBarBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
|
||
|
|
// 4.将自定义的YBTabBarButton对象
|
||
|
|
[self.tabbarBtnArray addObject:tabBarBtn];
|
||
|
|
[self.norImageArrM addObject:norImageUrl];
|
||
|
|
// 5.默认选中第0个按钮
|
||
|
|
if (self.tabbarBtnArray.count == 1) {
|
||
|
|
[self buttonClick:tabBarBtn];
|
||
|
|
[tabBarBtn.iconBtn setSelected:YES];
|
||
|
|
}
|
||
|
|
|
||
|
|
if ([title isEqualToString:QXText(@"消息")]) {
|
||
|
|
self.messageBtn = tabBarBtn;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
-(void)setUnReadNumber:(UInt64)unReadNumber{
|
||
|
|
_unReadNumber = unReadNumber;
|
||
|
|
[self.messageBtn.unreadView setNum:unReadNumber];
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 当前点击的按钮置为选中样式
|
||
|
|
#pragma mark - 设置选中的ybButton
|
||
|
|
- (void)JJDealSelectButton:(QXTabBarButton *)ybButton {
|
||
|
|
for (int i = 0; i < self.tabbarBtnArray.count; i++) {
|
||
|
|
QXTabBarButton *currentButton = self.tabbarBtnArray[i];
|
||
|
|
if (currentButton == ybButton) {
|
||
|
|
[currentButton.iconBtn setSelected:YES];
|
||
|
|
currentButton.titleLbl.textColor = QXConfig.themeColor;
|
||
|
|
} else {
|
||
|
|
[currentButton.iconBtn setSelected:NO];
|
||
|
|
currentButton.titleLbl.textColor = [[QXTabbarConfig shareInstance] norTitleColor];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#pragma mark - <QXTabBarButtonDelegate>
|
||
|
|
- (void)QXTabbarIconButtonClick:(QXTabBarButton *)button {
|
||
|
|
[self buttonClick:button];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 监听按钮点击
|
||
|
|
*/
|
||
|
|
- (void)buttonClick:(QXTabBarButton *)button {
|
||
|
|
// 1.通知代理
|
||
|
|
if ([self.delegate respondsToSelector:@selector(QXTabBar:didSelectedButtonFrom:to:)]) {
|
||
|
|
[self.delegate QXTabBar:self didSelectedButtonFrom:self.selectedButton.tag to:button.tag];
|
||
|
|
}
|
||
|
|
if (![QXGlobal shareGlobal].isLogin && button.tag == QXConfig.tabbarArray.count-1) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// 2.设置按钮的状态
|
||
|
|
self.selectedButton.selected = NO;
|
||
|
|
button.selected = YES;
|
||
|
|
self.selectedButton = button;
|
||
|
|
[self JJDealSelectButton:self.selectedButton];
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)layoutSubviews {
|
||
|
|
[super layoutSubviews];
|
||
|
|
|
||
|
|
// 按钮的frame数据
|
||
|
|
CGFloat buttonH = self.frame.size.height;
|
||
|
|
CGFloat buttonW = self.frame.size.width / self.tabbarBtnArray.count;
|
||
|
|
CGFloat buttonY = 0;
|
||
|
|
for (int index = 0; index<self.tabbarBtnArray.count; index++) {
|
||
|
|
// 1.取出按钮
|
||
|
|
QXTabBarButton *button = self.tabbarBtnArray[index];
|
||
|
|
// 2.设置按钮的frame
|
||
|
|
CGFloat buttonX = index * buttonW;
|
||
|
|
|
||
|
|
if(index==2 && self.centerCustom){
|
||
|
|
[button.titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||
|
|
make.centerX.equalTo(self);
|
||
|
|
make.top.equalTo(button.iconBtn.mas_bottom).offset([[QXTabbarConfig shareInstance] titleOffset]);
|
||
|
|
make.left.right.equalTo(self);
|
||
|
|
make.height.mas_equalTo([[QXTabbarConfig shareInstance]titleHeight]);
|
||
|
|
}];
|
||
|
|
button.frame = CGRectMake(buttonX, -18, buttonW, buttonH);
|
||
|
|
[button.iconBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||
|
|
make.centerX.equalTo(button.mas_centerX);
|
||
|
|
make.top.equalTo(button.mas_top);
|
||
|
|
make.height.width.mas_equalTo(65);
|
||
|
|
}];
|
||
|
|
}else{
|
||
|
|
|
||
|
|
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3.绑定tag
|
||
|
|
button.tag = index;
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@end
|
||
|
|
|
||
|
|
|