438 lines
16 KiB
Objective-C
438 lines
16 KiB
Objective-C
//
|
|
// CustomRoomBGCell.m
|
|
// YuMi
|
|
//
|
|
// Created by P on 2024/10/30.
|
|
//
|
|
|
|
#import "CustomRoomBGCell.h"
|
|
|
|
#import "SVGA.h"
|
|
#import "CustomRoomBGItemModel.h"
|
|
|
|
@interface CustomRoomBGCell ()
|
|
|
|
@property (nonatomic, strong) NetImageView *bgImageView;
|
|
@property (nonatomic, strong) UIView *selectedStateView;
|
|
@property (nonatomic, strong) UILabel *freeDefaultTagLabel;
|
|
@property (nonatomic, strong) UILabel *remainingDaysLabel;
|
|
@property (nonatomic, strong) UILabel *typeLabel;
|
|
@property (nonatomic, strong) UIButton *previewButton;
|
|
@property (nonatomic, strong) UILabel *pricePerDayLabel;
|
|
@property (nonatomic, strong) UIButton *actionButton;
|
|
@property (nonatomic, strong) UIImageView *bgTypeImageView;
|
|
@property (nonatomic, strong) UIButton *playButton;
|
|
@property (nonatomic, strong) UIButton *trashButton;
|
|
@property (nonatomic, strong) UIImageView *goldIcon;
|
|
@property (nonatomic, strong) CustomRoomBGItemModel *cellModel;
|
|
@property (nonatomic, strong) NSIndexPath *cellIndexPath;
|
|
@property (nonatomic, strong) SVGAImageView *svgaImageView;
|
|
@property (nonatomic, strong) SVGAParser *parser;
|
|
//@property (nonatomic, strong) SVGAVideoEntity *svgaEntity;
|
|
@end
|
|
|
|
@implementation CustomRoomBGCell
|
|
|
|
+ (void)registerTo:(UICollectionView *)collectionView {
|
|
NSString *cellID_free = [NSString stringWithFormat:@"%@%@", NSStringFromClass([self class]), @(0)];
|
|
NSString *cellID_pay = [NSString stringWithFormat:@"%@%@", NSStringFromClass([self class]), @(1)];
|
|
NSString *cellID_custom = [NSString stringWithFormat:@"%@%@", NSStringFromClass([self class]), @(2)];
|
|
[collectionView registerClass:[self class]
|
|
forCellWithReuseIdentifier:cellID_pay];
|
|
[collectionView registerClass:[self class]
|
|
forCellWithReuseIdentifier:cellID_free];
|
|
[collectionView registerClass:[self class]
|
|
forCellWithReuseIdentifier:cellID_custom];
|
|
}
|
|
|
|
+ (CustomRoomBGCell *)reuseFrom:(UICollectionView *)collectionView
|
|
model:(CustomRoomBGItemModel *)model
|
|
atIndexPath:(NSIndexPath *)indexPath {
|
|
NSString *cellID = [NSString stringWithFormat:@"%@%@", NSStringFromClass([self class]), @(model.type)];
|
|
CustomRoomBGCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID
|
|
forIndexPath:indexPath];
|
|
cell.cellIndexPath = indexPath;
|
|
cell.cellModel = model;
|
|
return cell;
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
if (self = [super initWithFrame:frame]) {
|
|
[self setupUI];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)prepareForReuse {
|
|
[super prepareForReuse];
|
|
[self pauseSVGA];
|
|
}
|
|
|
|
- (void)playSVGA {
|
|
self.bgImageView.hidden = YES;
|
|
self.svgaImageView.hidden = YES;
|
|
if ([self.cellModel.url.lowercaseString hasSuffix:@"svga"]) {
|
|
self.svgaImageView.hidden = NO;
|
|
if (self.svgaImageView.videoItem) {
|
|
[self.svgaImageView startAnimation];
|
|
} else {
|
|
NSLog(@"%@", self.cellModel.url);
|
|
@kWeakify(self);
|
|
[self.parser parseWithURL:[NSURL URLWithString:[self.cellModel.url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]
|
|
completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
|
|
@kStrongify(self);
|
|
self.svgaImageView.videoItem = videoItem;
|
|
[self.svgaImageView startAnimation];
|
|
} failureBlock:^(NSError * _Nullable error) {
|
|
NSLog(@"%@", error);
|
|
}];
|
|
}
|
|
} else {
|
|
self.bgImageView.hidden = NO;
|
|
self.bgImageView.imageUrl = self.cellModel.url;
|
|
}
|
|
}
|
|
- (void)pauseSVGA {
|
|
if ([self.cellModel.url.lowercaseString hasSuffix:@"svga"]) {
|
|
[self.svgaImageView stopAnimation]; // 停止动画
|
|
self.svgaImageView.videoItem = nil; // 清除视频资源
|
|
}
|
|
}
|
|
|
|
- (void)displayFreeDefaultTag:(BOOL)needDisplay {
|
|
self.freeDefaultTagLabel.hidden = !needDisplay;
|
|
}
|
|
|
|
- (void)displayPayRemainTag:(BOOL)needDisplay {
|
|
self.remainingDaysLabel.hidden = !needDisplay;
|
|
}
|
|
|
|
- (void)displayGIFTag:(BOOL)needDisplay {
|
|
self.bgTypeImageView.hidden = !needDisplay;
|
|
self.playButton.hidden = !needDisplay;
|
|
}
|
|
|
|
- (void)updateActionButtonToRenew {
|
|
[self.actionButton setTitle:YMLocalizedString(@"1.0.18_10") forState:UIControlStateNormal];
|
|
self.actionButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
|
|
[self.actionButton setCornerRadius:10
|
|
corners:(kCALayerMinXMinYCorner |
|
|
kCALayerMaxXMinYCorner |
|
|
kCALayerMinXMaxYCorner |
|
|
kCALayerMaxXMaxYCorner)
|
|
borderWidth:1
|
|
borderColor:[UIColor whiteColor]];
|
|
}
|
|
|
|
- (void)updateActionButtonToBuy {
|
|
[self.actionButton setTitle:YMLocalizedString(@"1.0.18_9") forState:UIControlStateNormal];
|
|
// 创建渐变图层
|
|
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
|
|
gradientLayer.colors = @[(__bridge id)UIColorFromRGB(0xE29030).CGColor,
|
|
(__bridge id)UIColorFromRGB(0xFCC074).CGColor];
|
|
gradientLayer.startPoint = CGPointMake(0.0, 0.0); // 顶部中央
|
|
gradientLayer.endPoint = CGPointMake(0.0, 1.0); // 底部中央
|
|
gradientLayer.frame = CGRectMake(0, 0, 72, 22); // 设置渐变图层大小
|
|
|
|
// 将渐变图层添加到按钮图层
|
|
[self.actionButton.layer insertSublayer:gradientLayer atIndex:0];
|
|
[self.actionButton setCornerRadius:10
|
|
corners:(kCALayerMinXMinYCorner |
|
|
kCALayerMaxXMinYCorner |
|
|
kCALayerMinXMaxYCorner |
|
|
kCALayerMaxXMaxYCorner)
|
|
borderWidth:0
|
|
borderColor:[UIColor clearColor]];
|
|
|
|
self.actionButton.userInteractionEnabled = NO;
|
|
}
|
|
|
|
- (void)setIsSelectedCell:(BOOL)isSelectedCell {
|
|
_isSelectedCell = isSelectedCell;
|
|
self.selectedStateView.hidden = !isSelectedCell;
|
|
}
|
|
|
|
- (void)setCellModel:(CustomRoomBGItemModel *)cellModel {
|
|
_cellModel = cellModel;
|
|
|
|
self.isSelectedCell = cellModel.isCur;
|
|
switch (cellModel.type) {
|
|
case RoomBGType_Free:
|
|
[self layoutFreeUI];
|
|
[self displayFreeDefaultTag:self.cellIndexPath.row == 0];
|
|
[self displayGIFTag:[cellModel.url hasSuffix:@"svga"]];
|
|
break;
|
|
case RoomBGType_Pay:
|
|
[self layoutPayUI];
|
|
[self setupBottomArea];
|
|
self.pricePerDayLabel.text = [cellModel pricePerDays];
|
|
[self displayGIFTag:[cellModel.url hasSuffix:@"svga"]];
|
|
[self displayPayRemainTag:cellModel.status == RoomBGStatus_Expired || cellModel.status == RoomBGStatus_Pass];
|
|
[self updateActionButtonToBuy];
|
|
switch (cellModel.status) {
|
|
case RoomBGStatus_Expired:
|
|
self.remainingDaysLabel.text = YMLocalizedString(@"1.0.18_7");
|
|
[self updateActionButtonToRenew];
|
|
break;
|
|
case RoomBGStatus_Reviewing:
|
|
self.remainingDaysLabel.text = @"";
|
|
break;
|
|
case RoomBGStatus_Pass:
|
|
self.remainingDaysLabel.text = [cellModel remainDays];
|
|
self.remainingDaysLabel.hidden = [NSString isEmpty:[cellModel remainDays]];
|
|
break;
|
|
case RoomBGStatus_Rejected:
|
|
self.remainingDaysLabel.text = @"";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case RoomBGType_Custom:
|
|
[self layoutCustomUI];
|
|
[self setupBottomArea];
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
- (void)layoutFreeUI {
|
|
[self.contentView addSubview:self.freeDefaultTagLabel];
|
|
[self.freeDefaultTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.leading.mas_equalTo(6);
|
|
make.height.mas_equalTo(20);
|
|
make.width.mas_greaterThanOrEqualTo(52);
|
|
}];
|
|
|
|
[self.contentView addSubview:self.bgTypeImageView];
|
|
[self.bgTypeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.trailing.top.mas_equalTo(self.contentView).inset(6);
|
|
make.size.mas_equalTo(CGSizeMake(27, 19));
|
|
}];
|
|
}
|
|
|
|
- (void)layoutPayUI {
|
|
[self.contentView addSubview:self.remainingDaysLabel];
|
|
[self.remainingDaysLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.leading.mas_equalTo(6);
|
|
make.height.mas_equalTo(20);
|
|
make.width.mas_greaterThanOrEqualTo(50);
|
|
}];
|
|
|
|
[self.contentView addSubview:self.playButton];
|
|
[self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.trailing.top.mas_equalTo(self.contentView).inset(6);
|
|
make.size.mas_equalTo(CGSizeMake(24, 19));
|
|
}];
|
|
|
|
[self.contentView addSubview:self.bgTypeImageView];
|
|
[self.bgTypeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerY.mas_equalTo(self.playButton);
|
|
make.trailing.mas_equalTo(self.playButton.mas_leading).offset(-6);
|
|
make.size.mas_equalTo(CGSizeMake(27, 19));
|
|
}];
|
|
}
|
|
|
|
- (void)layoutCustomUI {
|
|
[self.contentView addSubview:self.remainingDaysLabel];
|
|
[self.remainingDaysLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.leading.mas_equalTo(6);
|
|
make.height.mas_equalTo(20);
|
|
make.width.mas_greaterThanOrEqualTo(50);
|
|
}];
|
|
|
|
[self.contentView addSubview:self.trashButton];
|
|
[self.trashButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.trailing.top.mas_equalTo(self.contentView).inset(6);
|
|
make.size.mas_equalTo(CGSizeMake(24, 19));
|
|
}];
|
|
}
|
|
|
|
- (void)setupUI {
|
|
[self.contentView addSubview:self.bgImageView];
|
|
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self.contentView).offset(6);
|
|
make.top.leading.trailing.mas_equalTo(self.contentView);
|
|
make.height.mas_equalTo(kGetScaleWidth(180));
|
|
}];
|
|
|
|
[self.contentView addSubview:self.svgaImageView];
|
|
[self.svgaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self.contentView).offset(6);
|
|
make.top.leading.trailing.mas_equalTo(self.contentView);
|
|
make.height.mas_equalTo(kGetScaleWidth(180));
|
|
}];
|
|
|
|
[self.contentView addSubview:self.selectedStateView];
|
|
[self.selectedStateView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.mas_equalTo(self.bgImageView);
|
|
}];
|
|
}
|
|
|
|
- (void)setupBottomArea {
|
|
UIStackView *stack = [[UIStackView alloc] init];
|
|
stack.spacing = 3;
|
|
[self.contentView addSubview: stack];
|
|
[stack mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self.selectedStateView.mas_bottom).offset(6);
|
|
make.centerX.mas_equalTo(self.selectedStateView);
|
|
make.height.mas_equalTo(22);
|
|
}];
|
|
|
|
[stack addArrangedSubview:self.goldIcon];
|
|
[self.goldIcon mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.size.mas_equalTo(CGSizeMake(22, 22));
|
|
}];
|
|
|
|
[stack addArrangedSubview:self.pricePerDayLabel];
|
|
|
|
[self.contentView addSubview:self.actionButton];
|
|
[self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerX.mas_equalTo(self.contentView);
|
|
make.top.mas_equalTo(stack.mas_bottom).offset(6);
|
|
make.width.mas_greaterThanOrEqualTo(72);
|
|
make.height.mas_equalTo(22);
|
|
}];
|
|
}
|
|
|
|
#pragma mark -
|
|
- (void)didTapActionButton {
|
|
|
|
}
|
|
|
|
- (void)didTapPlayButton {
|
|
if (self.handleTapPlayButton) {
|
|
self.handleTapPlayButton(@"");
|
|
}
|
|
}
|
|
|
|
- (void)didTapTrashButton {
|
|
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
- (NetImageView *)bgImageView {
|
|
if (!_bgImageView) {
|
|
NetImageConfig * config = [[NetImageConfig alloc]init];
|
|
config.placeHolder = [UIImageConstant defaultEmptyPlaceholder];
|
|
_bgImageView = [[NetImageView alloc] initWithConfig:config];
|
|
_bgImageView.contentMode = UIViewContentModeScaleAspectFill;
|
|
_bgImageView.layer.masksToBounds = YES;
|
|
_bgImageView.layer.cornerRadius = 10;
|
|
}
|
|
return _bgImageView;
|
|
}
|
|
|
|
- (SVGAImageView *)svgaImageView {
|
|
if (!_svgaImageView) {
|
|
_svgaImageView = [[SVGAImageView alloc] init];
|
|
_svgaImageView.contentMode = UIViewContentModeScaleAspectFit;
|
|
_svgaImageView.loops = 0;
|
|
}
|
|
return _svgaImageView;
|
|
}
|
|
|
|
- (UIView *)selectedStateView {
|
|
if (!_selectedStateView) {
|
|
_selectedStateView = [[UIView alloc] init];
|
|
_selectedStateView.backgroundColor = [UIColor clearColor];
|
|
_selectedStateView.layer.cornerRadius = 10;
|
|
_selectedStateView.layer.borderColor = UIColorFromRGB(0xFF8C03).CGColor;
|
|
_selectedStateView.layer.borderWidth = 2;
|
|
_selectedStateView.layer.masksToBounds = YES;
|
|
_selectedStateView.hidden = YES;
|
|
}
|
|
return _selectedStateView;
|
|
}
|
|
|
|
- (UILabel *)remainingDaysLabel {
|
|
if (!_remainingDaysLabel) {
|
|
_remainingDaysLabel = [UILabel labelInitWithText:@"" font:kFontRegular(12) textColor:[UIColor whiteColor]];
|
|
_remainingDaysLabel.backgroundColor = UIColorFromRGB(0x696969);
|
|
_remainingDaysLabel.textAlignment = NSTextAlignmentCenter;
|
|
[_remainingDaysLabel setCornerRadius:10];
|
|
}
|
|
return _remainingDaysLabel;
|
|
}
|
|
|
|
- (UIImageView *)goldIcon {
|
|
if (!_goldIcon) {
|
|
_goldIcon = [[UIImageView alloc] initWithImage:kImage(@"moli_money_icon")];
|
|
}
|
|
|
|
return _goldIcon;
|
|
}
|
|
|
|
- (UILabel *)pricePerDayLabel {
|
|
if (!_pricePerDayLabel) {
|
|
_pricePerDayLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.18_8")
|
|
font:kFontRegular(14)
|
|
textColor:[UIColor whiteColor]];
|
|
}
|
|
return _pricePerDayLabel;
|
|
}
|
|
|
|
- (UIButton *)actionButton {
|
|
if (!_actionButton) {
|
|
_actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_actionButton setTitle:YMLocalizedString(@"1.0.18_9")
|
|
forState:UIControlStateNormal];
|
|
_actionButton.layer.cornerRadius = 10;
|
|
_actionButton.layer.masksToBounds = YES;
|
|
[_actionButton addTarget:self
|
|
action:@selector(didTapActionButton)
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _actionButton;
|
|
}
|
|
|
|
- (UIImageView *)bgTypeImageView {
|
|
if (!_bgTypeImageView) {
|
|
_bgTypeImageView = [[UIImageView alloc] initWithImage:kImage(@"custom_bg_gif")];
|
|
}
|
|
return _bgTypeImageView;
|
|
}
|
|
|
|
- (UIButton *)playButton {
|
|
if (!_playButton) {
|
|
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_playButton setImage:kImage(@"custom_bg_play") forState:UIControlStateNormal];
|
|
[_playButton addTarget:self
|
|
action:@selector(didTapPlayButton)
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _playButton;
|
|
}
|
|
|
|
- (UIButton *)trashButton {
|
|
if (!_trashButton) {
|
|
_trashButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_trashButton setImage:kImage(@"custom_bg_trash") forState:UIControlStateNormal];
|
|
[_trashButton addTarget:self
|
|
action:@selector(didTapTrashButton)
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _trashButton;
|
|
}
|
|
|
|
- (UILabel *)freeDefaultTagLabel {
|
|
if (!_freeDefaultTagLabel) {
|
|
_freeDefaultTagLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.18_17") font:kFontRegular(12) textColor:[UIColor whiteColor]];
|
|
_freeDefaultTagLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
|
|
_freeDefaultTagLabel.textAlignment = NSTextAlignmentCenter;
|
|
[_freeDefaultTagLabel setCornerRadius:10];
|
|
}
|
|
return _freeDefaultTagLabel;
|
|
}
|
|
|
|
- (SVGAParser *)parser {
|
|
if (!_parser) {
|
|
_parser = [[SVGAParser alloc] init];
|
|
}
|
|
return _parser;
|
|
}
|
|
|
|
@end
|