更新 MedalsCyclePagerCell 和 MedalsViewController 以优化勋章展示和自动轮播功能,调整可见性管理逻辑,确保在视图切换时正确处理播放状态,保持代码结构一致性。

This commit is contained in:
edwinQQQ
2025-06-23 18:43:17 +08:00
parent c0500397b6
commit 286e68b5e3
3 changed files with 230 additions and 35 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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];
}
///