Files
peko-ios/YuMi/Modules/YMNewHome/View/CustomView/XPNewHomeHeadView.m
2024-05-31 19:56:25 +08:00

179 lines
7.2 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// XPNewHomeHeadView.m
// YuMi
//
// Created by duoban on 2023/9/1.
//
#import "XPNewHomeHeadView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "XPNewHomeItemCell.h"
#import "PIPageControl.h"
@interface XPNewHomeHeadView ()<SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *collectionView;
///轮播图
@property (nonatomic, strong) SDCycleScrollView *pi_BannerView;
@property(nonatomic,strong) PIPageControl *pageControl;
@end
@implementation XPNewHomeHeadView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self installUI];
[self installConstraints];
}
return self;
}
-(void)installUI{
[self addSubview:self.collectionView];
[self addSubview:self.pi_BannerView];
[self addSubview:self.pageControl];
}
-(void)installConstraints{
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(0);
make.trailing.mas_equalTo(0);
make.top.mas_equalTo(kGetScaleWidth(0));
make.height.mas_equalTo(kGetScaleWidth(80));
}];
[self.pi_BannerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.collectionView.mas_bottom).mas_offset(kGetScaleWidth(10));
make.leading.trailing.equalTo(self).inset(kGetScaleWidth(14));
make.height.mas_equalTo(kGetScaleWidth(80));
}];
}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
HomeBannerInfoModel * bannerInfo = [self.bannerList safeObjectAtIndex1:index];
if(bannerInfo != nil && self.delegate && [self.delegate respondsToSelector:@selector(selectBannerListWithModel:)]){
[self.delegate selectBannerListWithModel:bannerInfo];
}
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
self.pageControl.currentPage = index;
}
#pragma mark - Getters And Setters
- (void)setBannerList:(NSArray<HomeBannerInfoModel *> *)bannerList {
_bannerList = bannerList;
if (_bannerList.count > 0) {
NSMutableArray * array = [NSMutableArray array];
[_bannerList enumerateObjectsUsingBlock:^(HomeBannerInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.bannerPic.length > 0) {
[array addObject:obj.bannerPic];
}
}];
if (array.count > 0) {
self.pi_BannerView.imageURLStringsGroup = array;
CGFloat width = array.count * kGetScaleWidth(7) - kGetScaleWidth(3);
[self.pageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-kGetScaleWidth(4));
make.height.mas_equalTo(kGetScaleWidth(4));
make.centerX.equalTo(self);
make.width.mas_equalTo(width);
}];
self.pageControl.numberOfPages = array.count;
self.pageControl.currentPage = 0;
[self.pi_BannerView autoScroll];
}
_pi_BannerView.hidden = NO;
}else{
_pi_BannerView.hidden = YES;
}
}
- (void)setItemList:(NSArray *)itemList{
_itemList = itemList;
self.collectionView.scrollEnabled = _itemList.count > 2;
self.collectionView.hidden = _itemList.count == 0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
if (_itemList.count == 0){
[self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(0);
make.trailing.mas_equalTo(0);
make.top.mas_equalTo(kGetScaleWidth(0));
make.height.mas_equalTo(kGetScaleWidth(0));
}];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.itemList.count > 0 ? self.itemList.count : 2;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPNewHomeItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPNewHomeItemCell class]) forIndexPath:indexPath];
if(indexPath.row < self.itemList.count){
cell.itmeModel = self.itemList[indexPath.row];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
PIHomeItemModel *model = [self.itemList safeObjectAtIndex1:indexPath.row];
if(model != nil && self.delegate && [self.delegate respondsToSelector:@selector(selectItemWithModel:)]){
[self.delegate selectItemWithModel:model];
}
}
#pragma mark - 懒加载
- (UICollectionView *)collectionView{
if (!_collectionView) {
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(0, kGetScaleWidth(15), 0, kGetScaleWidth(15));
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = kGetScaleWidth(8);
layout.itemSize = CGSizeMake(kGetScaleWidth(168), kGetScaleWidth(80));
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.pagingEnabled = NO;
_collectionView.hidden = YES;
_collectionView.scrollEnabled = NO;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerClass:[XPNewHomeItemCell class] forCellWithReuseIdentifier:NSStringFromClass([XPNewHomeItemCell class])];
_collectionView.showsVerticalScrollIndicator = NO;
}
return _collectionView;
}
#pragma mark - 懒加载
- (SDCycleScrollView *)pi_BannerView {
if (!_pi_BannerView) {
_pi_BannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImageConstant defalutBannerPlaceholder]];
_pi_BannerView.backgroundColor = [UIColor clearColor];
_pi_BannerView.layer.cornerRadius = 10;
_pi_BannerView.layer.masksToBounds = YES;
_pi_BannerView.showPageControl = NO;
_pi_BannerView.autoScrollTimeInterval = 5.0;
_pi_BannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
_pi_BannerView.tag = 9000001;
_pi_BannerView.hidden = YES;
// SDCycleScrollView没有适配阿语在RTL下会乱滚都用LTR算了
_pi_BannerView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
for (UIView *subView in _pi_BannerView.subviews) {
subView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
}
}
return _pi_BannerView;
}
- (PIPageControl *)pageControl{
if(!_pageControl){
_pageControl = [[PIPageControl alloc]init];
_pageControl.currentPageIndicatorTintColor = UIColorFromRGB(0x9168FA);
_pageControl.pageIndicatorTintColor = UIColorRGBAlpha(0xB3B3C3, 0.4);
}
return _pageControl;
}
@end