更新 MedalsCyclePagerCell 和 MedalsViewController 以优化勋章展示和自动轮播功能,调整可见性管理逻辑,确保在视图切换时正确处理播放状态,保持代码结构一致性。
This commit is contained in:
@@ -45,6 +45,8 @@
|
||||
}
|
||||
|
||||
- (void)setupUI {
|
||||
self.isVisible = YES;
|
||||
|
||||
self.backgroundColor = [UIColor clearColor];
|
||||
self.contentView.backgroundColor = [UIColor clearColor];
|
||||
|
||||
@@ -96,7 +98,7 @@
|
||||
// 重置状态
|
||||
self.mp4Path = nil;
|
||||
self.imagePath = nil;
|
||||
self.isVisible = NO;
|
||||
self.isVisible = YES;
|
||||
|
||||
// 清空图片
|
||||
self.imageView.image = nil;
|
||||
|
@@ -29,7 +29,7 @@ typedef enum : NSInteger {
|
||||
MedalsCenterDisplayType_Square
|
||||
} MedalsCenterDisplayType;
|
||||
|
||||
@interface MedalsViewController () <MedalsPresenterProtocol, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>
|
||||
@interface MedalsViewController () <MedalsPresenterProtocol, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, TYCyclePagerViewDataSource, TYCyclePagerViewDelegate, UICollectionViewDelegate>
|
||||
|
||||
@property (nonatomic, strong) UserInfoModel *userInfo;
|
||||
@property (nonatomic, copy) NSArray <UIButton *> *centerTabButtons;
|
||||
@@ -52,6 +52,7 @@ typedef enum : NSInteger {
|
||||
@property (nonatomic, assign) MedalsCenterDisplayType displayType;
|
||||
|
||||
@property (nonatomic, strong) UserMedalsModel *userMedalsModel;
|
||||
@property (nonatomic, copy) NSMutableArray <MedalSeriesVo *>*squareMedalVo;
|
||||
|
||||
@property (nonatomic, strong) UIImageView *otherBG;
|
||||
@property (nonatomic, strong) NetImageView *otherAvatar;
|
||||
@@ -61,6 +62,10 @@ typedef enum : NSInteger {
|
||||
@property (nonatomic, strong) UILabel *otherNameLabel;
|
||||
@property (nonatomic, strong) UILabel *otherCountLabel;
|
||||
|
||||
// 自动轮播相关属性
|
||||
@property (nonatomic, strong) NSTimer *autoScrollTimer;
|
||||
@property (nonatomic, assign) NSInteger currentAutoScrollIndex;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MedalsViewController
|
||||
@@ -99,6 +104,10 @@ typedef enum : NSInteger {
|
||||
return [[MedalsPresenter alloc] init];
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self stopAutoScroll];
|
||||
}
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
|
||||
@@ -106,11 +115,112 @@ typedef enum : NSInteger {
|
||||
[self setupData];
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated {
|
||||
[super viewDidAppear:animated];
|
||||
|
||||
// 确保可见的 cell 开始播放
|
||||
[self startVisibleCellsPlayback];
|
||||
|
||||
// 如果有佩戴的勋章,再次确保居中显示
|
||||
if (self.useMedals.count > 0 && !self.medalsCyclePagerView.hidden) {
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self.medalsCyclePagerView scrollToItemAtIndex:0 animate:NO];
|
||||
});
|
||||
}
|
||||
|
||||
// 重新启动自动轮播(如果有多个勋章且在广场页面)
|
||||
if (self.useMedals.count > 1 && !self.medalsCyclePagerView.hidden && self.displayType == MedalsCenterDisplayType_Square) {
|
||||
[self startAutoScroll:self.currentAutoScrollIndex];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewWillDisappear:(BOOL)animated {
|
||||
[super viewWillDisappear:animated];
|
||||
|
||||
// 停止所有播放
|
||||
[self stopAllCellsPlayback];
|
||||
|
||||
// 停止自动轮播
|
||||
[self stopAutoScroll];
|
||||
}
|
||||
|
||||
- (void)stopAllCellsPlayback {
|
||||
// 停止所有 cell 的播放
|
||||
NSArray *visibleCells = self.medalsCyclePagerView.collectionView.visibleCells;
|
||||
for (UICollectionViewCell *cell in visibleCells) {
|
||||
if ([cell isKindOfClass:[MedalsCyclePagerCell class]]) {
|
||||
[(MedalsCyclePagerCell *)cell didEndDisplaying];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - 自动轮播方法
|
||||
|
||||
/**
|
||||
* 启动自动轮播
|
||||
* @param userSelectedIndex 用户选择的起始索引,如果无效则默认从0开始
|
||||
*/
|
||||
- (void)startAutoScroll:(NSInteger)userSelectedIndex {
|
||||
[self stopAutoScroll]; // 先停止之前的定时器
|
||||
|
||||
// 设置当前索引
|
||||
if (userSelectedIndex >= 0 && userSelectedIndex < self.useMedals.count) {
|
||||
self.currentAutoScrollIndex = userSelectedIndex;
|
||||
} else {
|
||||
self.currentAutoScrollIndex = 0;
|
||||
}
|
||||
|
||||
NSTimeInterval time = 5;
|
||||
#if DEBUG
|
||||
time = 2; // DEBUG 模式下缩短间隔便于测试
|
||||
#endif
|
||||
|
||||
// 总是创建新的定时器(因为已经在上面停止了旧的)
|
||||
self.autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:time
|
||||
target:self
|
||||
selector:@selector(autoScrollToNextItem)
|
||||
userInfo:nil
|
||||
repeats:YES];
|
||||
}
|
||||
|
||||
- (void)stopAutoScroll {
|
||||
if (self.autoScrollTimer) {
|
||||
[self.autoScrollTimer invalidate];
|
||||
self.autoScrollTimer = nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)autoScrollToNextItem {
|
||||
if (self.useMedals.count <= 1) {
|
||||
[self stopAutoScroll];
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算下一个索引,实现循环滚动
|
||||
self.currentAutoScrollIndex = (self.currentAutoScrollIndex + 1) % self.useMedals.count;
|
||||
|
||||
// 滚动到下一个位置
|
||||
[self.medalsCyclePagerView scrollToItemAtIndex:self.currentAutoScrollIndex animate:YES];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tab 切换时重置自动滚动状态
|
||||
* 停止当前定时器,重置索引为0,准备从第一个元素重新开始轮播
|
||||
*/
|
||||
- (void)resetAutoScrollOnTabChange {
|
||||
// 停止当前的自动滚动定时器
|
||||
[self stopAutoScroll];
|
||||
|
||||
// 重置滚动索引为 0,确保切换 tab 后从第一个元素开始显示
|
||||
self.currentAutoScrollIndex = 0;
|
||||
|
||||
// 注意:新的自动滚动将在数据更新后的 _updateWearingInfo 方法中重新启动
|
||||
}
|
||||
|
||||
#pragma mark - UI
|
||||
- (void)setupUI {
|
||||
[self setupBackground];
|
||||
if (self.displayType != MedalsCenterDisplayType_Other) {
|
||||
[self setupWearingButton];
|
||||
if (self.userInfo.medals.medalCount == 0) {
|
||||
[self setupEmptyUserMedals];
|
||||
} else {
|
||||
@@ -125,6 +235,10 @@ typedef enum : NSInteger {
|
||||
[self setupEmptyView];
|
||||
|
||||
[self setupNavigationBar];
|
||||
|
||||
if (self.displayType != MedalsCenterDisplayType_Other) {
|
||||
[self setupWearingButton];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setupNavigationBar {
|
||||
@@ -282,6 +396,7 @@ typedef enum : NSInteger {
|
||||
[self.medalsCyclePagerView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.center.mas_equalTo(self.emptyUserMedalButton);
|
||||
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
|
||||
// make.width.mas_equalTo(184*3);
|
||||
make.height.mas_equalTo(self.emptyUserMedalButton);
|
||||
}];
|
||||
|
||||
@@ -295,14 +410,14 @@ typedef enum : NSInteger {
|
||||
}
|
||||
|
||||
- (void)setupWearingUserMedals {
|
||||
// 添加 TYCyclePagerView 用于显示佩戴的勋章
|
||||
[self.view addSubview:self.medalsCyclePagerView];
|
||||
[self.medalsCyclePagerView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.mas_equalTo(100);
|
||||
make.centerX.mas_equalTo(self.view);
|
||||
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
|
||||
make.height.mas_equalTo(184);
|
||||
}];
|
||||
// // 添加 TYCyclePagerView 用于显示佩戴的勋章
|
||||
// [self.view addSubview:self.medalsCyclePagerView];
|
||||
// [self.medalsCyclePagerView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
// make.top.mas_equalTo(100);
|
||||
// make.centerX.mas_equalTo(self.view);
|
||||
//// make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
|
||||
// make.width.height.mas_equalTo(184);
|
||||
// }];
|
||||
|
||||
[self.view addSubview:self.medalDescLabel];
|
||||
self.medalDescLabel.text = @"显示过期时间";//YMLocalizedString(@"20.20.61_text_6");
|
||||
@@ -458,6 +573,7 @@ typedef enum : NSInteger {
|
||||
- (void)squareMedalsSuccess:(NSArray <MedalSeriesVo *>*)squareMedalsModel {
|
||||
[self endReresh];
|
||||
[self _updateDataSource:squareMedalsModel];
|
||||
[self _updateWearingInfo];
|
||||
}
|
||||
|
||||
- (void)squareMedalsFailure {
|
||||
@@ -486,11 +602,73 @@ typedef enum : NSInteger {
|
||||
}
|
||||
|
||||
- (void)_updateWearingInfo {
|
||||
self.useMedals = self.userMedalsModel.useMedals;
|
||||
|
||||
if (self.displayType == MedalsCenterDisplayType_Mine) {
|
||||
self.useMedals = self.userMedalsModel.useMedals;
|
||||
#if DEBUG
|
||||
NSMutableArray *arr = [self.userMedalsModel.useMedals mutableCopy];
|
||||
[arr addObjectsFromArray:self.userMedalsModel.useMedals];
|
||||
[arr addObjectsFromArray:self.userMedalsModel.useMedals];
|
||||
[arr addObjectsFromArray:self.userMedalsModel.useMedals];
|
||||
[arr addObjectsFromArray:self.userMedalsModel.useMedals];
|
||||
self.useMedals = arr.copy;
|
||||
#endif
|
||||
} else if (self.displayType == MedalsCenterDisplayType_Square) {
|
||||
NSArray *arr = [NSArray array];
|
||||
NSMutableArray *editArr = [NSMutableArray array];
|
||||
switch (self.currentTabType) {
|
||||
case MedalsCenterTab_TaskMedals:
|
||||
arr = self.datasourceTaskMedals.copy;
|
||||
break;
|
||||
case MedalsCenterTab_ActivityMedals:
|
||||
arr = self.datasourceActivityMedals.copy;
|
||||
break;
|
||||
case MedalsCenterTab_GloryMedals:
|
||||
arr = self.datasourceGloryMedals.copy;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
for (MedalSeriesVo *seriesVO in arr) {
|
||||
MedalSeriesItemVo *item = [seriesVO.medalSeries xpSafeObjectAtIndex:0];
|
||||
if (item) {
|
||||
MedalVo *medal = [item.medalVos xpSafeObjectAtIndex:0];
|
||||
if (medal) {
|
||||
[editArr addObject:medal];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.useMedals = editArr.copy;
|
||||
|
||||
#if DEBUG
|
||||
NSMutableArray *arr_demo = editArr;
|
||||
[arr_demo addObjectsFromArray:editArr];
|
||||
[arr_demo addObjectsFromArray:editArr];
|
||||
[arr_demo addObjectsFromArray:editArr];
|
||||
[arr_demo addObjectsFromArray:editArr];
|
||||
self.useMedals = arr_demo.copy;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (self.displayType != MedalsCenterDisplayType_Other) {
|
||||
if (self.useMedals.count > 0 ) {
|
||||
self.emptyUserMedalButton.hidden = YES;
|
||||
self.medalsCyclePagerView.hidden = NO;
|
||||
[self.medalsCyclePagerView reloadData];
|
||||
if (self.useMedals.count > 1 && self.displayType == MedalsCenterDisplayType_Square) {
|
||||
// 启动自动轮播,从第一个位置开始
|
||||
[self startAutoScroll:0];
|
||||
|
||||
// 确保滚动到第一个位置
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self.medalsCyclePagerView scrollToItemAtIndex:0 animate:NO];
|
||||
});
|
||||
} else {
|
||||
// 停止自动轮播
|
||||
[self stopAutoScroll];
|
||||
}
|
||||
} else {
|
||||
self.emptyUserMedalButton.hidden = NO;
|
||||
self.medalsCyclePagerView.hidden = YES;
|
||||
@@ -498,6 +676,17 @@ typedef enum : NSInteger {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (void)startVisibleCellsPlayback {
|
||||
// 获取所有可见的 cell 并启动播放
|
||||
NSArray *visibleCells = self.medalsCyclePagerView.collectionView.visibleCells;
|
||||
for (UICollectionViewCell *cell in visibleCells) {
|
||||
if ([cell isKindOfClass:[MedalsCyclePagerCell class]]) {
|
||||
[(MedalsCyclePagerCell *)cell willDisplay];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_updateOtherInfo:(UserMedalsModel *)userModel {
|
||||
if (self.displayType == MedalsCenterDisplayType_Other) {
|
||||
self.otherAvatar.imageUrl = userModel.avatar;
|
||||
@@ -579,6 +768,10 @@ typedef enum : NSInteger {
|
||||
}
|
||||
sender.selected = YES;
|
||||
self.currentTabType = sender.tag;
|
||||
|
||||
// 切换 tab 时重置自动滚动
|
||||
[self resetAutoScrollOnTabChange];
|
||||
|
||||
[self loadMedalsList:self.currentTabType page:1];
|
||||
}
|
||||
|
||||
@@ -607,18 +800,25 @@ typedef enum : NSInteger {
|
||||
|
||||
// 处理 cell 的可见性
|
||||
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
if ([cell isKindOfClass:[MedalsCollectionViewCell class]]) {
|
||||
[(MedalsCollectionViewCell *)cell willDisplay];
|
||||
if (collectionView == self.medalsCollectionView) {
|
||||
if ([cell isKindOfClass:[MedalsCollectionViewCell class]]) {
|
||||
[(MedalsCollectionViewCell *)cell willDisplay];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
if ([cell isKindOfClass:[MedalsCollectionViewCell class]]) {
|
||||
[(MedalsCollectionViewCell *)cell didEndDisplaying];
|
||||
if (collectionView == self.medalsCollectionView) {
|
||||
if ([cell isKindOfClass:[MedalsCollectionViewCell class]]) {
|
||||
[(MedalsCollectionViewCell *)cell didEndDisplaying];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
if (collectionView == self.medalsCyclePagerView.collectionView) {
|
||||
return;
|
||||
}
|
||||
MedalsDetailView *view = [[MedalsDetailView alloc] initWithFrame:self.view.bounds];
|
||||
view.detailItemVo = [self loadModel:indexPath.item];
|
||||
[self.view addSubview:view];
|
||||
@@ -666,23 +866,12 @@ typedef enum : NSInteger {
|
||||
}
|
||||
|
||||
- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
|
||||
// 处理滚动事件,暂停之前的 cell,恢复当前的 cell
|
||||
// 获取可见的 cells
|
||||
NSArray *visibleCells = pageView.collectionView.visibleCells;
|
||||
// 用户手动滚动时,更新当前索引,重新开始自动轮播计时
|
||||
self.currentAutoScrollIndex = toIndex;
|
||||
|
||||
// 简化处理:遍历所有可见的 cells,暂停非当前 index 的 cell
|
||||
for (UICollectionViewCell *cell in visibleCells) {
|
||||
if ([cell isKindOfClass:[MedalsCyclePagerCell class]]) {
|
||||
NSIndexPath *indexPath = [pageView.collectionView indexPathForCell:cell];
|
||||
if (indexPath) {
|
||||
NSInteger cellIndex = indexPath.item;
|
||||
if (cellIndex == toIndex) {
|
||||
[(MedalsCyclePagerCell *)cell willDisplay];
|
||||
} else {
|
||||
[(MedalsCyclePagerCell *)cell didEndDisplaying];
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果有多个勋章且在广场页面,重新启动自动轮播定时器
|
||||
if (self.useMedals.count > 1 && self.displayType == MedalsCenterDisplayType_Square) {
|
||||
[self startAutoScroll:toIndex];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -846,11 +1035,15 @@ typedef enum : NSInteger {
|
||||
_medalsCyclePagerView = [[TYCyclePagerView alloc] init];
|
||||
_medalsCyclePagerView.dataSource = self;
|
||||
_medalsCyclePagerView.delegate = self;
|
||||
_medalsCyclePagerView.backgroundColor = [UIColor clearColor];
|
||||
_medalsCyclePagerView.backgroundColor = [UIColor redColor];//[UIColor clearColor];
|
||||
_medalsCyclePagerView.isInfiniteLoop = NO;
|
||||
_medalsCyclePagerView.clipsToBounds = NO;
|
||||
// _medalsCyclePagerView.autoScrollInterval = 0; // 禁用自动滚动
|
||||
[_medalsCyclePagerView registerClass:[MedalsCyclePagerCell class]
|
||||
forCellWithReuseIdentifier:@"MedalsCyclePagerCell"];
|
||||
|
||||
// 设置内部 collectionView 的代理,用于处理 cell 的显示和隐藏
|
||||
// _medalsCyclePagerView.collectionView.delegate = self;
|
||||
}
|
||||
return _medalsCyclePagerView;
|
||||
}
|
||||
|
@@ -258,8 +258,8 @@
|
||||
/// @param completion 完成
|
||||
/// @param roomUid 房间uid
|
||||
+ (void)requestNewUserInRoomGift:(HttpRequestHelperCompletion)completion roomUid:(NSString *)roomUid {
|
||||
NSString * fang = [NSString stringFromBase64String:@"Z2lmdC9uZXdVc2VyL2luUm9vbQ=="];///gift/newUser/inRoom
|
||||
[self makeRequest:fang method:HttpRequestHelperMethodGET completion:completion, __FUNCTION__, roomUid, nil];
|
||||
// NSString * fang = [NSString stringFromBase64String:@"Z2lmdC9uZXdVc2VyL2luUm9vbQ=="];///gift/newUser/inRoom
|
||||
// [self makeRequest:fang method:HttpRequestHelperMethodGET completion:completion, __FUNCTION__, roomUid, nil];
|
||||
}
|
||||
|
||||
/// 获取红包信息
|
||||
|
Reference in New Issue
Block a user