Files
peko-ios/YuMi/Modules/YMMine/View/Medals/MedalsDetailView.m

268 lines
8.8 KiB
Objective-C

//
// MedalsDetailView.m
// YuMi
//
// Created by P on 2025/6/18.
//
#import "MedalsDetailView.h"
#import "MedalsModel.h"
#import <QGVAPWrapView.h>
#import "XPRoomGiftAnimationParser.h"
#import "MedalsLevelIndicatorView.h"
@interface MedalsDetailView ()
@property (nonatomic, strong) UIImageView *bgLightImageView;
@property(nonatomic, copy) NSString *imagePath;
@property(nonatomic, copy) NSString *mp4Path;
@property(nonatomic, strong) NetImageView *imageView;
@property(nonatomic, strong) VAPView *mp4View;
@property(nonatomic, strong) XPRoomGiftAnimationParser *mp4Parser;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subLabel;
@property (nonatomic, strong) MedalsLevelIndicatorView *levelIndicatorView;
@property (nonatomic, strong) MedalSeriesItemVo *currentSeriesItemVO;
@property (nonatomic, strong) MedalVos *displayModel;
@end
@implementation MedalsDetailView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupUI];
}
return self;
}
- (void)setupUI {
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
[self addSubview:self.bgLightImageView];
[self.bgLightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self);
make.height.mas_equalTo(self.bgLightImageView.mas_width);
}];
self.imageView = [[NetImageView alloc] init];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:self.imageView];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self);
make.top.mas_equalTo(kGetScaleWidth(135));
make.size.mas_equalTo(CGSizeMake(228, 228));
}];
[self addSubview:self.mp4View];
[self.mp4View mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.imageView);
}];
self.titleLabel = [UILabel labelInitWithText:@"" font:kFontSemibold(20) textColor:[UIColor whiteColor]];
self.subLabel = [UILabel labelInitWithText:@"" font:kFontRegular(12) textColor:[UIColor colorWithWhite:1 alpha:0.6]];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.subLabel.textAlignment = NSTextAlignmentCenter;
self.subLabel.numberOfLines = 2;
[self addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self);
make.top.mas_equalTo(self.imageView.mas_bottom).offset(8);
make.leading.trailing.mas_equalTo(self).inset(13);
}];
[self addSubview:self.subLabel];
[self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self);
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(12);
make.leading.trailing.mas_equalTo(self).inset(3);
}];
// 添加等级指示器
self.levelIndicatorView = [[MedalsLevelIndicatorView alloc] init];
[self addSubview:self.levelIndicatorView];
[self.levelIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self);
make.top.mas_equalTo(self.subLabel).offset(24);
make.leading.trailing.mas_greaterThanOrEqualTo(self).inset(8);
make.height.mas_equalTo(26); // 增加高度以适应圆点和文本
}];
// 设置等级选择回调
@kWeakify(self);
self.levelIndicatorView.levelSelectedBlock = ^(NSInteger level) {
@kStrongify(self);
// 处理等级选择事件
if (self.currentSeriesItemVO && level <= self.currentSeriesItemVO.medalVos.count) {
MedalVos *selectedMedalVo = [self.currentSeriesItemVO.medalVos xpSafeObjectAtIndex:level - 1];
if (selectedMedalVo) {
self.displayModel = selectedMedalVo;
[self updateDisplayWithCurrentModel];
}
}
};
// 在 levelIndicatorView 底部添加一个视图作为关闭按钮区域
UIView *closeButtonView = [[UIView alloc] init];
closeButtonView.backgroundColor = [UIColor clearColor];
[self addSubview:closeButtonView];
[closeButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.levelIndicatorView.mas_bottom).offset(20);
make.bottom.leading.trailing.mas_equalTo(self);
}];
// 添加点击手势到关闭按钮区域
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackgroundTap:)];
[closeButtonView addGestureRecognizer:tapGesture];
}
- (void)setDetailItemVo:(MedalSeriesVo *)detailItemVo {
_detailItemVo = detailItemVo;
self.currentSeriesItemVO = [detailItemVo.medalSeries xpSafeObjectAtIndex:0];
self.displayModel = [self.currentSeriesItemVO.medalVos xpSafeObjectAtIndex:0];
[self.levelIndicatorView setupWithMaxLevel:self.currentSeriesItemVO.medalLevel];
[self.levelIndicatorView setSelectedLevel:1 animated:NO];
// 设置指示器类型为带图片
self.levelIndicatorView.indicatorType = MedalsLevelIndicatorTypeWithImage;
// 为每个等级设置对应的图片
for (NSInteger i = 0; i < self.currentSeriesItemVO.medalVos.count; i++) {
MedalVos *medalVo = [self.currentSeriesItemVO.medalVos xpSafeObjectAtIndex:i];
if (medalVo) {
[self.levelIndicatorView setImageUrl:medalVo.picUrl forLevel:i + 1];
}
}
[self updateDisplayWithCurrentModel];
}
- (void)updateDisplayWithCurrentModel {
if (self.displayModel) {
if ([self.displayModel.picUrl hasSuffix:@"mp4"]) {
[self setMp4Path:self.displayModel.picUrl];
} else {
[self setImagePath:self.displayModel.picUrl];
}
self.titleLabel.text = self.displayModel.name;
self.subLabel.text = self.displayModel.medalDesc;
}
}
- (void)setImagePath:(NSString *)imagePath {
// 停止之前的 mp4 播放
[self stopMP4Playback];
_imagePath = imagePath;
self.mp4View.hidden = YES;
self.imageView.hidden = NO;
self.imageView.imageUrl = imagePath;
}
- (void)setMp4Path:(NSString *)mp4Path {
// 如果是相同的 mp4 路径,不需要重新加载
if ([_mp4Path isEqualToString:mp4Path]) {
return;
}
// 停止之前的 mp4 播放
[self stopMP4Playback];
_mp4Path = mp4Path;
self.mp4View.hidden = NO;
self.imageView.hidden = YES;
if (!_mp4Parser) {
self.mp4Parser = [[XPRoomGiftAnimationParser alloc] init];
}
@kWeakify(self);
[self.mp4Parser parseWithURL:mp4Path
completionBlock:^(NSString * _Nullable videoUrl) {
@kStrongify(self);
if (![NSString isEmpty:videoUrl]) {
[self.mp4View playHWDMP4:videoUrl repeatCount:-1 delegate:nil];
}
} failureBlock:^(NSError * _Nullable error) {
NSLog(@"Failed to parse mp4: %@", error);
}];
}
#pragma mark - MP4 播放控制
- (void)stopMP4Playback {
if (self.mp4View) {
[self.mp4View stopHWDMP4];
self.mp4View.tag = 0; // 重置播放状态标记
}
}
- (void)pauseMP4Playback {
if (self.mp4View && !self.mp4View.hidden) {
[self.mp4View pauseHWDMP4];
}
}
- (void)resumeMP4Playback {
if (self.mp4View && !self.mp4View.hidden && self.mp4Path) {
if (self.mp4View.tag == 1) { // 已准备好但尚未播放
@kWeakify(self);
[self.mp4Parser parseWithURL:self.mp4Path
completionBlock:^(NSString * _Nullable videoUrl) {
@kStrongify(self);
if (![NSString isEmpty:videoUrl]) {
[self.mp4View playHWDMP4:videoUrl repeatCount:-1 delegate:nil];
}
} failureBlock:nil];
} else {
[self.mp4View resumeHWDMP4];
}
}
}
#pragma mark -
- (UIImageView *)bgLightImageView {
if (!_bgLightImageView) {
_bgLightImageView = [[UIImageView alloc] initWithImage:kImage(@"medals_lights")];
}
return _bgLightImageView;
}
- (VAPView *)mp4View {
if (!_mp4View) {
_mp4View = [[VAPView alloc] init];
_mp4View.contentMode = UIViewContentModeScaleAspectFit;
}
return _mp4View;
}
// 修改处理背景点击的方法名称,使其更符合实际功能
- (void)handleBackgroundTap:(UITapGestureRecognizer *)gesture {
[self removeFromSuperview];
}
// 重写 removeFromSuperview 方法,确保在移除前停止 MP4 播放
- (void)removeFromSuperview {
// 停止 MP4 播放并释放资源
[self stopMP4Playback];
self.mp4Parser = nil;
[super removeFromSuperview];
}
// 添加 dealloc 方法确保资源被正确释放
- (void)dealloc {
// 停止 MP4 播放
[self stopMP4Playback];
// 清理资源
self.mp4Parser = nil;
NSLog(@"MedalsDetailView dealloc");
}
@end