240 lines
8.8 KiB
Objective-C
240 lines
8.8 KiB
Objective-C
//
|
||
// EventCenterOfficialCell.m
|
||
// YuMi
|
||
//
|
||
// Created by P on 2025/4/29.
|
||
//
|
||
|
||
#import "EventCenterOfficialCell.h"
|
||
#import "HomeBannerInfoModel.h"
|
||
|
||
@interface EventCenterOfficialCell()
|
||
|
||
@property (nonatomic, strong) NetImageView *coverImageView;
|
||
@property (nonatomic, strong) UILabel *titleLabel;
|
||
@property (nonatomic, strong) UIStackView *countdownView;
|
||
@property (nonatomic, strong) UILabel *countdownPrefixLabel; // Countdown: 前缀标签
|
||
@property (nonatomic, strong) UILabel *daysNumberLabel; // 天数数字标签
|
||
@property (nonatomic, strong) UILabel *hourNumberLabel; // 天数数字标签
|
||
@property (nonatomic, strong) UILabel *minuteNumberLabel; // 天数数字标签
|
||
@property (nonatomic, strong) UILabel *secondNumberLabel; // 天数数字标签
|
||
@property (nonatomic, strong) UILabel *daysSuffixLabel; // days 后缀标签
|
||
@property (nonatomic, strong) UILabel *colonSuffixLabel_1; // ":" 标签
|
||
@property (nonatomic, strong) UILabel *colonSuffixLabel_2; // ":" 后缀标签
|
||
@property (nonatomic, strong) NSTimer *countdownTimer; // 倒计时定时器
|
||
|
||
@end
|
||
|
||
@implementation EventCenterOfficialCell
|
||
|
||
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
||
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
|
||
|
||
self.selectionStyle = UITableViewCellSelectionStyleNone;
|
||
self.contentView.backgroundColor = [UIColor whiteColor];
|
||
|
||
[self.contentView addSubview:self.coverImageView];
|
||
[self.contentView addSubview:self.titleLabel];
|
||
[self.contentView addSubview:self.countdownView];
|
||
|
||
[self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.mas_equalTo(self.contentView).offset(6);
|
||
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
|
||
make.height.mas_equalTo(80);
|
||
}];
|
||
|
||
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.mas_equalTo(self.coverImageView.mas_bottom).offset(4);
|
||
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
|
||
make.height.mas_equalTo(26);
|
||
}];
|
||
|
||
[self.countdownView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(0);
|
||
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
|
||
make.height.mas_equalTo(26);
|
||
}];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (void)setCellModel:(HomeBannerInfoModel *)cellModel {
|
||
_cellModel = cellModel;
|
||
self.coverImageView.imageUrl = cellModel.bannerPic;
|
||
self.titleLabel.text = cellModel.bannerName;
|
||
|
||
// 假设从模型中获取倒计时天数
|
||
NSInteger days = 30; // 这里应该从cellModel中获取实际的倒计时天数
|
||
[self updateCountdownWithDays:days];
|
||
}
|
||
|
||
#pragma mark -
|
||
- (NetImageView *)coverImageView {
|
||
if (!_coverImageView) {
|
||
NetImageConfig *config = [[NetImageConfig alloc] init];
|
||
config.placeHolder = [UIImageConstant defaultBannerPlaceholder];
|
||
_coverImageView = [[NetImageView alloc] initWithConfig:config];
|
||
[_coverImageView setCornerRadius:12];
|
||
}
|
||
return _coverImageView;
|
||
}
|
||
|
||
- (UILabel *)titleLabel {
|
||
if (!_titleLabel) {
|
||
_titleLabel = [UILabel labelInitWithText:@"" font:kFontSemibold(14) textColor:UIColorFromRGB(0x313131)];
|
||
}
|
||
return _titleLabel;
|
||
}
|
||
|
||
- (UIStackView *)countdownView {
|
||
if (!_countdownView) {
|
||
_countdownView = [[UIStackView alloc] initWithArrangedSubviews:@[
|
||
self.countdownPrefixLabel,
|
||
self.daysNumberLabel,
|
||
self.daysSuffixLabel,
|
||
self.hourNumberLabel,
|
||
self.colonSuffixLabel_1,
|
||
self.minuteNumberLabel,
|
||
self.colonSuffixLabel_2,
|
||
self.secondNumberLabel,
|
||
[UIView new]
|
||
]];
|
||
_countdownView.spacing = 6;
|
||
_countdownView.backgroundColor = [UIColor clearColor];
|
||
|
||
[self.daysNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.greaterThanOrEqualTo(@30);
|
||
}];
|
||
[self.hourNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.greaterThanOrEqualTo(@30);
|
||
}];
|
||
[self.minuteNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.greaterThanOrEqualTo(@30);
|
||
}];
|
||
[self.secondNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.greaterThanOrEqualTo(@30);
|
||
}];
|
||
}
|
||
return _countdownView;
|
||
}
|
||
|
||
- (UILabel *)countdownPrefixLabel {
|
||
if (!_countdownPrefixLabel) {
|
||
_countdownPrefixLabel = [UILabel labelInitWithText:@"Countdown: " font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
}
|
||
return _countdownPrefixLabel;
|
||
}
|
||
|
||
- (UILabel *)daysNumberLabel {
|
||
if (!_daysNumberLabel) {
|
||
_daysNumberLabel = [UILabel labelInitWithText:@"00" font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
_daysNumberLabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.15];
|
||
_daysNumberLabel.layer.cornerRadius = 6;
|
||
_daysNumberLabel.layer.masksToBounds = YES;
|
||
_daysNumberLabel.textAlignment = NSTextAlignmentCenter;
|
||
}
|
||
return _daysNumberLabel;
|
||
}
|
||
|
||
- (UILabel *)hourNumberLabel {
|
||
if (!_hourNumberLabel) {
|
||
_hourNumberLabel = [UILabel labelInitWithText:@"00" font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
_hourNumberLabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.15];
|
||
_hourNumberLabel.layer.cornerRadius = 6;
|
||
_hourNumberLabel.layer.masksToBounds = YES;
|
||
_hourNumberLabel.textAlignment = NSTextAlignmentCenter;
|
||
}
|
||
return _hourNumberLabel;
|
||
}
|
||
|
||
- (UILabel *)minuteNumberLabel {
|
||
if (!_minuteNumberLabel) {
|
||
_minuteNumberLabel = [UILabel labelInitWithText:@"00" font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
_minuteNumberLabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.15];
|
||
_minuteNumberLabel.layer.cornerRadius = 6;
|
||
_minuteNumberLabel.layer.masksToBounds = YES;
|
||
_minuteNumberLabel.textAlignment = NSTextAlignmentCenter;
|
||
}
|
||
return _minuteNumberLabel;
|
||
}
|
||
|
||
- (UILabel *)secondNumberLabel {
|
||
if (!_secondNumberLabel) {
|
||
_secondNumberLabel = [UILabel labelInitWithText:@"00" font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
_secondNumberLabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.15];
|
||
_secondNumberLabel.layer.cornerRadius = 6;
|
||
_secondNumberLabel.layer.masksToBounds = YES;
|
||
_secondNumberLabel.textAlignment = NSTextAlignmentCenter;
|
||
}
|
||
return _secondNumberLabel;
|
||
}
|
||
|
||
- (UILabel *)daysSuffixLabel {
|
||
if (!_daysSuffixLabel) {
|
||
_daysSuffixLabel = [UILabel labelInitWithText:@"days" font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
}
|
||
return _daysSuffixLabel;
|
||
}
|
||
|
||
- (UILabel *)colonSuffixLabel_1 {
|
||
if (!_colonSuffixLabel_1) {
|
||
_colonSuffixLabel_1 = [UILabel labelInitWithText:@":" font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
}
|
||
return _colonSuffixLabel_1;
|
||
}
|
||
|
||
- (UILabel *)colonSuffixLabel_2 {
|
||
if (!_colonSuffixLabel_2) {
|
||
_colonSuffixLabel_2 = [UILabel labelInitWithText:@":" font:kFontSemibold(16) textColor:UIColorFromRGB(0xff9900)];
|
||
}
|
||
return _colonSuffixLabel_2;
|
||
}
|
||
|
||
- (void)updateCountdownWithDays:(NSInteger)days {
|
||
// 更新天数标签
|
||
self.daysNumberLabel.text = [NSString stringWithFormat:@"%02ld", (long)days];
|
||
|
||
// 如果有定时器,先停止
|
||
[self stopCountdownTimer];
|
||
|
||
// 如果天数大于0,启动定时器进行倒计时
|
||
if (days > 0) {
|
||
[self startCountdownTimerWithDays:days];
|
||
}
|
||
}
|
||
|
||
- (void)startCountdownTimerWithDays:(NSInteger)days {
|
||
// 创建一个每天触发一次的定时器
|
||
// 注意:实际应用中,可能需要更精确的倒计时逻辑,这里仅作示例
|
||
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:86400 target:self selector:@selector(updateDaysCount) userInfo:nil repeats:YES];
|
||
}
|
||
|
||
- (void)updateDaysCount {
|
||
// 获取当前显示的天数
|
||
NSInteger currentDays = [self.daysNumberLabel.text integerValue];
|
||
|
||
// 如果天数大于0,减少一天
|
||
if (currentDays > 0) {
|
||
currentDays--;
|
||
self.daysNumberLabel.text = [NSString stringWithFormat:@"%02ld", (long)currentDays];
|
||
}
|
||
|
||
// 如果天数为0,停止定时器
|
||
if (currentDays == 0) {
|
||
[self stopCountdownTimer];
|
||
}
|
||
}
|
||
|
||
- (void)stopCountdownTimer {
|
||
if (self.countdownTimer && [self.countdownTimer isValid]) {
|
||
[self.countdownTimer invalidate];
|
||
self.countdownTimer = nil;
|
||
}
|
||
}
|
||
|
||
- (void)dealloc {
|
||
// 确保在Cell被释放时停止定时器
|
||
[self stopCountdownTimer];
|
||
}
|
||
@end
|