2023-09-01 15:52:10 +08:00
|
|
|
//
|
|
|
|
// XPNewHomeHeadView.m
|
|
|
|
// YuMi
|
|
|
|
//
|
|
|
|
// Created by duoban on 2023/9/1.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "XPNewHomeHeadView.h"
|
2023-09-01 18:58:41 +08:00
|
|
|
#import <SDCycleScrollView/SDCycleScrollView.h>
|
2023-09-05 11:45:21 +08:00
|
|
|
#import "XPNewHomeItemCell.h"
|
2023-10-08 16:46:12 +08:00
|
|
|
#import "PIPageControl.h"
|
2023-09-05 11:45:21 +08:00
|
|
|
@interface XPNewHomeHeadView ()<SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
|
|
|
|
@property(nonatomic,strong) UICollectionView *collectionView;
|
2023-09-01 18:58:41 +08:00
|
|
|
///轮播图
|
|
|
|
@property (nonatomic, strong) SDCycleScrollView *pi_BannerView;
|
2023-10-08 16:46:12 +08:00
|
|
|
@property(nonatomic,strong) PIPageControl *pageControl;
|
2023-09-01 18:58:41 +08:00
|
|
|
@end
|
2023-09-01 15:52:10 +08:00
|
|
|
@implementation XPNewHomeHeadView
|
|
|
|
|
2023-09-01 18:58:41 +08:00
|
|
|
-(instancetype)initWithFrame:(CGRect)frame{
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if(self){
|
|
|
|
[self installUI];
|
|
|
|
[self installConstraints];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
-(void)installUI{
|
2023-09-05 11:45:21 +08:00
|
|
|
[self addSubview:self.collectionView];
|
2023-09-01 18:58:41 +08:00
|
|
|
[self addSubview:self.pi_BannerView];
|
2023-10-08 16:46:12 +08:00
|
|
|
[self addSubview:self.pageControl];
|
2023-09-01 18:58:41 +08:00
|
|
|
}
|
|
|
|
-(void)installConstraints{
|
2023-09-05 11:45:21 +08:00
|
|
|
|
|
|
|
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
make.leading.mas_equalTo(0);
|
|
|
|
make.trailing.mas_equalTo(0);
|
2023-09-01 18:58:41 +08:00
|
|
|
make.top.mas_equalTo(kGetScaleWidth(0));
|
2023-09-05 11:45:21 +08:00
|
|
|
make.height.mas_equalTo(kGetScaleWidth(80));
|
2023-09-01 18:58:41 +08:00
|
|
|
}];
|
|
|
|
[self.pi_BannerView mas_makeConstraints:^(MASConstraintMaker *make) {
|
2023-09-05 11:45:21 +08:00
|
|
|
make.top.equalTo(self.collectionView.mas_bottom).mas_offset(kGetScaleWidth(10));
|
2023-09-01 18:58:41 +08:00
|
|
|
make.leading.trailing.equalTo(self).inset(kGetScaleWidth(14));
|
|
|
|
make.height.mas_equalTo(kGetScaleWidth(80));
|
|
|
|
}];
|
|
|
|
|
2023-10-08 16:46:12 +08:00
|
|
|
|
2023-09-01 18:58:41 +08:00
|
|
|
}
|
|
|
|
#pragma mark - SDCycleScrollViewDelegate
|
|
|
|
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
|
|
|
|
HomeBannerInfoModel * bannerInfo = [self.bannerList safeObjectAtIndex1:index];
|
2023-09-05 11:45:21 +08:00
|
|
|
if(bannerInfo != nil && self.delegate && [self.delegate respondsToSelector:@selector(selectBannerListWithModel:)]){
|
|
|
|
[self.delegate selectBannerListWithModel:bannerInfo];
|
|
|
|
}
|
|
|
|
|
2023-09-01 18:58:41 +08:00
|
|
|
}
|
2023-10-08 16:46:12 +08:00
|
|
|
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
|
|
|
|
self.pageControl.currentPage = index;
|
|
|
|
}
|
2023-09-01 18:58:41 +08:00
|
|
|
#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;
|
2023-10-08 16:46:12 +08:00
|
|
|
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;
|
|
|
|
|
2023-09-01 18:58:41 +08:00
|
|
|
[self.pi_BannerView autoScroll];
|
|
|
|
}
|
|
|
|
_pi_BannerView.hidden = NO;
|
|
|
|
}else{
|
|
|
|
_pi_BannerView.hidden = YES;
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 11:45:21 +08:00
|
|
|
- (void)setItemList:(NSArray *)itemList{
|
|
|
|
_itemList = itemList;
|
|
|
|
self.collectionView.scrollEnabled = _itemList.count > 2;
|
|
|
|
[self.collectionView reloadData];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (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];
|
2023-09-01 18:58:41 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-05 11:45:21 +08:00
|
|
|
#pragma mark - 懒加载
|
|
|
|
- (UICollectionView *)collectionView{
|
|
|
|
if (!_collectionView) {
|
|
|
|
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout 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.scrollEnabled = NO;
|
|
|
|
_collectionView.backgroundColor = [UIColor clearColor];
|
|
|
|
[_collectionView registerClass:[XPNewHomeItemCell class] forCellWithReuseIdentifier:NSStringFromClass([XPNewHomeItemCell class])];
|
|
|
|
_collectionView.showsVerticalScrollIndicator = NO;
|
2023-09-01 18:58:41 +08:00
|
|
|
}
|
2023-09-05 11:45:21 +08:00
|
|
|
return _collectionView;
|
2023-09-01 18:58:41 +08:00
|
|
|
}
|
2023-09-05 11:45:21 +08:00
|
|
|
#pragma mark - 懒加载
|
|
|
|
|
2023-09-01 18:58:41 +08:00
|
|
|
- (SDCycleScrollView *)pi_BannerView {
|
|
|
|
if (!_pi_BannerView) {
|
2023-10-08 16:46:12 +08:00
|
|
|
_pi_BannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImageConstant defalutBannerPlaceholder]];
|
2023-09-01 18:58:41 +08:00
|
|
|
_pi_BannerView.backgroundColor = [UIColor clearColor];
|
|
|
|
_pi_BannerView.layer.cornerRadius = 10;
|
|
|
|
_pi_BannerView.layer.masksToBounds = YES;
|
2023-10-08 16:46:12 +08:00
|
|
|
_pi_BannerView.showPageControl = NO;
|
2023-09-01 18:58:41 +08:00
|
|
|
_pi_BannerView.autoScrollTimeInterval = 5.0;
|
|
|
|
_pi_BannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
|
|
|
|
_pi_BannerView.tag = 9000001;
|
|
|
|
_pi_BannerView.hidden = YES;
|
|
|
|
}
|
|
|
|
return _pi_BannerView;
|
2023-09-01 15:52:10 +08:00
|
|
|
}
|
2023-10-08 16:46:12 +08:00
|
|
|
- (PIPageControl *)pageControl{
|
|
|
|
if(!_pageControl){
|
|
|
|
_pageControl = [[PIPageControl alloc]init];
|
|
|
|
_pageControl.currentPageIndicatorTintColor = UIColorFromRGB(0x9168FA);
|
|
|
|
_pageControl.pageIndicatorTintColor = UIColorRGBAlpha(0xB3B3C3, 0.4);
|
|
|
|
}
|
|
|
|
return _pageControl;
|
|
|
|
}
|
2023-09-01 15:52:10 +08:00
|
|
|
|
|
|
|
@end
|