Files
keyboard/keyBoard/Class/Me/V/KBSkinBottomActionView.m
2025-12-11 16:39:22 +08:00

175 lines
5.9 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// KBSkinBottomActionView.m
// keyBoard
//
//
#import "KBSkinBottomActionView.h"
@interface KBSkinBottomActionView ()
@property (nonatomic, strong) UIView *contentView; // 内部容器,使内容整体居中
@property (nonatomic, strong) UIStackView *stackView; // 水平排列 Title/Icon/Price
@property (nonatomic, strong) UILabel *titleLabel; // 左侧标题
@property (nonatomic, strong) UIImageView *coinImageView; // 中间图标(可选)
@property (nonatomic, strong) UILabel *priceLabel; // 右侧价格
@end
@implementation KBSkinBottomActionView
+ (CGFloat)preferredHeight { return 45.0; }
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor colorWithHex:KBColorValue];
self.layer.masksToBounds = YES; // 圆角生效
// 高亮态轻微透明,突出点击感
[self addTarget:self action:@selector(onTouchDown) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(onTouchUp) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
// 内部容器,承载三个元素,容器整体水平/垂直居中
[self addSubview:self.contentView];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.centerY.equalTo(self);
// 防止文本过长时超出左右边界
make.left.greaterThanOrEqualTo(self).offset(16);
make.right.lessThanOrEqualTo(self).offset(-16);
}];
// 三个元素放进容器左右顺序Title - Icon - Price
[self.contentView addSubview:self.stackView];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView);
}];
[self.stackView addArrangedSubview:self.titleLabel];
[self.stackView addArrangedSubview:self.coinImageView];
[self.stackView addArrangedSubview:self.priceLabel];
if (@available(iOS 11.0, *)) {
[self.stackView setCustomSpacing:8 afterView:self.titleLabel];
[self.stackView setCustomSpacing:6 afterView:self.coinImageView];
}
[self.coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(18);
}];
// 默认文案
self.titleText = @"Download";
self.priceText = @"20";
UIImage *img = [UIImage systemImageNamed:@"circle.fill"];
self.iconImage = img; // 若项目没有金币图标,用系统占位(黄色)
self.coinImageView.tintColor = [UIColor colorWithRed:1.0 green:0.85 blue:0.2 alpha:1.0];
self.showsPrice = YES;
// 点击回调(可选)
[self addTarget:self action:@selector(handleTap) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
// 圆角随高度自适应
self.layer.cornerRadius = CGRectGetHeight(self.bounds) * 0.5;
}
#pragma mark - Public
- (void)configWithTitle:(nullable NSString *)title price:(nullable NSString *)price icon:(nullable UIImage *)icon {
if (title.length) self.titleText = title;
if (price.length) self.priceText = price;
if (icon) self.iconImage = icon;
}
#pragma mark - Actions
- (void)onTouchDown { self.alpha = 0.85; }
- (void)onTouchUp { self.alpha = 1.0; }
- (void)handleTap {
if (self.tapHandler) { self.tapHandler(); }
}
#pragma mark - Setters
- (void)setTitleText:(NSString *)titleText {
_titleText = [titleText copy];
self.titleLabel.text = _titleText;
}
- (void)setPriceText:(NSString *)priceText {
_priceText = [priceText copy];
if (self.showsPrice) {
self.priceLabel.text = _priceText;
}
}
- (void)setIconImage:(UIImage *)iconImage {
_iconImage = iconImage;
self.coinImageView.image = _iconImage;
}
- (void)setShowsPrice:(BOOL)showsPrice {
if (_showsPrice == showsPrice) { return; }
_showsPrice = showsPrice;
self.coinImageView.hidden = !_showsPrice;
self.priceLabel.hidden = !_showsPrice;
self.priceLabel.text = _showsPrice ? self.priceText : @"";
if (@available(iOS 11.0, *)) {
[self.stackView setCustomSpacing:_showsPrice ? 8 : 0 afterView:self.titleLabel];
}
}
#pragma mark - Lazy
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [KBFont medium:16];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor clearColor];
// 让容器尽量包裹内容
[_contentView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[_contentView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}
return _contentView;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.spacing = 6;
}
return _stackView;
}
- (UIImageView *)coinImageView {
if (!_coinImageView) {
_coinImageView = [[UIImageView alloc] init];
_coinImageView.contentMode = UIViewContentModeScaleAspectFit;
_coinImageView.clipsToBounds = YES;
}
return _coinImageView;
}
- (UILabel *)priceLabel {
if (!_priceLabel) {
_priceLabel = [[UILabel alloc] init];
_priceLabel.font = [KBFont medium:16];
_priceLabel.textColor = [UIColor whiteColor];
_priceLabel.textAlignment = NSTextAlignmentCenter;
}
return _priceLabel;
}
@end