Files
featherVoice/QXLive/Tabbar/TabbarCustomView/QXTabBarButton.m

123 lines
4.1 KiB
Mathematica
Raw Normal View History

2025-08-08 10:49:36 +08:00
//
// QXTabBarButton.m
// FreeTabbar_JoeJin
//
// Created by J-Mac on 2024/5/24.
// Copyright © 2024 JoeJin-QQ:853105953. All rights reserved.
//
#import "QXTabBarButton.h"
@implementation QXTabBarButton
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubviews];
}
return self;
}
- (void)addSubviews {
[self addSubview:self.iconBtn];
[self addSubview:self.titleLbl];
[self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self.mas_top).offset(-1);
make.height.width.mas_equalTo(32);
}];
[self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self.iconBtn.mas_bottom).offset(0);
make.left.right.equalTo(self);
make.height.mas_equalTo(12);
}];
[self addTarget:self action:@selector(iconButtonClick:) forControlEvents:(UIControlEventTouchDown)];
self.unreadView = [[TUIUnReadView alloc] init];
[self.unreadView setNum:0];
[self addSubview:self.unreadView];
[self.unreadView.unReadLabel sizeToFit];
[self.unreadView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self);
make.left.equalTo(self.iconBtn.mas_right).offset(-10);
make.width.mas_equalTo(kScale375(20));
make.height.mas_equalTo(kScale375(20));
}];
[self.unreadView.unReadLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.unreadView);
make.size.mas_equalTo(self.unreadView.unReadLabel);
}];
self.unreadView.layer.cornerRadius = kScale375(10);
[self.unreadView.layer masksToBounds];
}
#pragma mark --
- (void)iconButtonClick:(QXTabBarButton *)ybButton {
// if (ybButton.titleLbl.text.length == 0) {
[self heartAnimationWithView:ybButton.iconBtn];
// }
if ([self.delegate respondsToSelector:@selector(QXTabbarIconButtonClick:)]) {
[self.delegate QXTabbarIconButtonClick:self];
}
}
#pragma mark -- lazy load
- (UIButton *)iconBtn {
if (!_iconBtn) {
_iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//_iconBtn.backgroundColor = [UIColor cyanColor];
_iconBtn.contentMode = UIViewContentModeScaleAspectFill;
_iconBtn.userInteractionEnabled = NO;
}
return _iconBtn;
}
- (UILabel *)titleLbl {
if (!_titleLbl) {
_titleLbl = [[UILabel alloc] init];
_titleLbl.font = [UIFont systemFontOfSize:[[QXTabbarConfig shareInstance] titleFont]];
//_titleLbl.textColor = KColorFromRGB(0x916d55);
_titleLbl.textColor = [[QXTabbarConfig shareInstance] norTitleColor];
_titleLbl.textAlignment = NSTextAlignmentCenter;
}
return _titleLbl;
}
/// tabbar
/// @param title
- (void)setTabBarImageUrl:(NSString *)imageUrl selectedImg:(nonnull NSString *)imageName title:(nonnull NSString *)title {
self.titleLbl.text = title;
2025-08-14 09:40:25 +08:00
if ([imageUrl hasPrefix:@"http"] || [imageUrl hasPrefix:@"https"]) {
[self.iconBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:imageUrl] forState:UIControlStateNormal placeholderImage:nil];
}else{
[self.iconBtn setBackgroundImage:[UIImage imageNamed:imageUrl] forState:UIControlStateNormal];
}
if ([imageName hasPrefix:@"http"] || [imageName hasPrefix:@"https"]) {
[self.iconBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:imageName] forState:UIControlStateSelected placeholderImage:nil];
}else{
[self.iconBtn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
}
2025-08-08 10:49:36 +08:00
}
- (void)heartAnimationWithView:(UIView*)view{
CABasicAnimation *anima = [CABasicAnimation animation];
anima.keyPath = @"transform.scale";
anima.fromValue = @1.2;
anima.toValue = @0.5;
anima.repeatCount = 1;
anima.duration = 0.1;
anima.autoreverses = YES;
anima.removedOnCompletion = YES;
[view.layer addAnimation:anima forKey:nil];
}
@end