Files
keyboard/keyBoard/Class/Pay/V/KBVipSubscribeCell.m
2025-11-14 19:48:15 +08:00

163 lines
6.2 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.

//
// KBVipSubscribeCell.m
// keyBoard
//
// 中文注释:订阅选项样式,右侧有选中图标,使用 mas 布局 + 懒加载。
//
#import "KBVipSubscribeCell.h"
@interface KBVipSubscribeCell ()
@property (nonatomic, strong) UIView *cardView; // 白色卡片背景
@property (nonatomic, strong) UILabel *titleLabel; // “Monthly Subscription”
@property (nonatomic, strong) UILabel *priceLabel; // “$4.49”
@property (nonatomic, strong) UILabel *strikeLabel; // 删除线原价
@property (nonatomic, strong) UIButton *selectButton; // 右侧选择按钮
@property (nonatomic, strong) CAShapeLayer *borderLayer; // 选中边框
@end
@implementation KBVipSubscribeCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.contentView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.cardView];
[self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView);
}];
[self.cardView addSubview:self.titleLabel];
[self.cardView addSubview:self.priceLabel];
[self.cardView addSubview:self.strikeLabel];
[self.cardView addSubview:self.selectButton];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.cardView).offset(16);
make.top.equalTo(self.cardView).offset(16);
}];
[self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.titleLabel);
make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
}];
[self.strikeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.priceLabel.mas_right).offset(10);
make.centerY.equalTo(self.priceLabel);
}];
[self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.cardView);
make.right.equalTo(self.cardView).offset(-16);
make.width.height.mas_equalTo(28);
}];
// 边框层(选中时显示主题绿)
self.borderLayer = [CAShapeLayer layer];
self.borderLayer.strokeColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;
// 使用透明填充,避免遮挡内部子视图;只显示描边
self.borderLayer.fillColor = UIColor.clearColor.CGColor;
self.borderLayer.lineWidth = 1.5;
// 放到底层,避免盖住 label/button修复滚动后偶现内容被遮挡变空白
[self.cardView.layer insertSublayer:self.borderLayer atIndex:0];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat radius = 16;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.cardView.bounds cornerRadius:radius];
self.cardView.layer.cornerRadius = radius;
self.cardView.layer.masksToBounds = YES;
self.borderLayer.frame = self.cardView.bounds;
self.borderLayer.path = path.CGPath;
}
- (void)prepareForReuse {
[super prepareForReuse];
[self applySelected:NO animated:NO];
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
[self applySelected:selected animated:NO];
}
- (void)configTitle:(NSString *)title price:(NSString *)price strike:(nullable NSString *)strike {
self.titleLabel.text = title.length ? title : @"Monthly Subscription";
self.priceLabel.text = price.length ? price : @"$4.49";
self.strikeLabel.hidden = (strike.length == 0);
if (strike.length) {
// 加删除线
NSDictionary *attr = @{
NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
NSForegroundColorAttributeName: [UIColor colorWithWhite:0.7 alpha:1.0]
};
self.strikeLabel.attributedText = [[NSAttributedString alloc] initWithString:strike attributes:attr];
}
}
- (void)applySelected:(BOOL)selected animated:(BOOL)animated {
UIImage *img = [UIImage imageNamed:(selected ? @"pay_circle_sel" : @"pay_circle_normal")];
[self.selectButton setImage:img forState:UIControlStateNormal];
CGColorRef color = (selected ? [UIColor colorWithHex:KBColorValue].CGColor : [UIColor colorWithWhite:0.9 alpha:1.0].CGColor);
void (^changes)(void) = ^{
self.borderLayer.strokeColor = color;
self.cardView.layer.shadowOpacity = selected ? 0.12 : 0.0;
self.cardView.layer.shadowColor = [UIColor colorWithHex:KBColorValue].CGColor;
self.cardView.layer.shadowRadius = selected ? 8 : 0;
self.cardView.layer.shadowOffset = CGSizeMake(0, selected ? 4 : 0);
};
if (animated) {
[UIView animateWithDuration:0.15 animations:changes];
} else {
changes();
}
}
#pragma mark - Lazy
- (UIView *)cardView {
if (!_cardView) {
_cardView = [UIView new];
_cardView.backgroundColor = [UIColor whiteColor];
}
return _cardView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel new];
_titleLabel.text = @"Monthly Subscription";
_titleLabel.textColor = [UIColor colorWithHex:KBBlackValue];
_titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
}
return _titleLabel;
}
- (UILabel *)priceLabel {
if (!_priceLabel) {
_priceLabel = [UILabel new];
_priceLabel.text = @"$4.49";
_priceLabel.textColor = [UIColor colorWithHex:KBBlackValue];
_priceLabel.font = [UIFont systemFontOfSize:28 weight:UIFontWeightBold];
}
return _priceLabel;
}
- (UILabel *)strikeLabel {
if (!_strikeLabel) {
_strikeLabel = [UILabel new];
_strikeLabel.text = @"$4.49";
_strikeLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
_strikeLabel.textColor = [UIColor colorWithWhite:0.7 alpha:1.0];
}
return _strikeLabel;
}
- (UIButton *)selectButton {
if (!_selectButton) {
_selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_selectButton setImage:[UIImage imageNamed:@"pay_circle_normal"] forState:UIControlStateNormal];
_selectButton.userInteractionEnabled = NO; // 由外层控制选中
_selectButton.contentMode = UIViewContentModeCenter;
}
return _selectButton;
}
@end