123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- //
- // LDSuperBrandView.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/12/10.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "LDSuperBrandView.h"
- #import "LDSuperBrandCollectionCell.h"
- #import "LDSuperBrandMoreCell.h"
- static NSString *cellId = @"cellID";
- static NSString *moreCellId = @"moreCellId";
- @interface LDSuperBrandView ()
- <
- UICollectionViewDelegate,
- UICollectionViewDataSource,
- UICollectionViewDelegateFlowLayout,
- UIScrollViewDelegate
- >{
- NSInteger offX;
- BOOL returnFlag;
- NSArray *dataArr;
- }
- @property (nonatomic, strong) UIView *bgView;
- @property (nonatomic, strong) UIImageView *topImgView;
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSTimer *timer;
- @end
- @implementation LDSuperBrandView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor whiteColor];
- [self initSubViews];
- }
- return self;
- }
- - (void)initSubViews {
- self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
- [self addSubview:self.bgView];
- self.bgView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
-
- self.topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,self.bgView.width, self.bgView.width*36/365)];
- self.topImgView.image = [UIImage imageNamed:@"childSuper"];
- self.topImgView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
- [self.bgView addSubview:self.topImgView];
-
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
- flowLayout.minimumLineSpacing = 5;
- flowLayout.minimumInteritemSpacing = 0;
- flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.topImgView.bottom, self.bgView.width, 100) collectionViewLayout:flowLayout];
- self.collectionView.delegate = self;
- self.collectionView.dataSource = self;
- self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
- self.collectionView.showsVerticalScrollIndicator = NO;
- self.collectionView.showsHorizontalScrollIndicator = NO;
- [self.collectionView registerClass:[LDSuperBrandCollectionCell class] forCellWithReuseIdentifier:cellId];
- [self.collectionView registerClass:[LDSuperBrandMoreCell class] forCellWithReuseIdentifier:moreCellId];
- [self.bgView addSubview:self.collectionView];
- }
- //- (void)startTimer {
- // if (!self.timer) {
- // self.timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(autoNextPage) userInfo:nil repeats:YES];
- // [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
- // }
- //}
- //
- //- (void)stopTimer {
- //
- // if (self.timer) {
- // [self.timer invalidate];
- // self.timer = nil;
- // }
- //}
- //- (void)autoNextPage {
- // CGSize contentSize = self.collectionView.contentSize;
- // CGFloat OffsetX = self.collectionView.contentOffset.x;
- // BOOL isBottom = (contentSize.width - OffsetX) <= self.collectionView.frame.size.width;
- //
- // if (isBottom) {
- // returnFlag = YES;
- // }
- // if (offX <= 0) {
- // returnFlag = NO;
- // }
- //
- // if (returnFlag) {
- // offX--;
- // }else {
- // offX++;
- // }
- //
- // [self.collectionView setContentOffset:CGPointMake(offX, 0) animated:YES];
- //}
- //#pragma mark --将要开始拖拽
- //- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- // [self stopTimer];
- //}
- //
- //#pragma mark --结束拖拽
- //- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
- // [self startTimer];
- //}
- //
- //
- //#pragma mark --将要结束拖拽
- //- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
- //
- // CGSize contentSize = self.collectionView.contentSize;
- // BOOL isBottom = (contentSize.width - targetContentOffset->x) <= self.collectionView.frame.size.width;
- //
- // if (isBottom) {
- // returnFlag = YES;
- // }
- // if (offX <= 0) {
- // returnFlag = NO;
- // }
- //
- // offX = targetContentOffset->x;
- //
- //}
- - (void)setSuperBrandData:(NSArray *)list {
- dataArr = list.mutableCopy;
- [self.collectionView reloadData];
- // if (self.timer) {
- // [self stopTimer];
- // }
- // [self startTimer];
- }
- #pragma mark ======
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- if (dataArr.count > 0) {
- return dataArr.count+1;
- }
- return dataArr.count;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return CGSizeMake(95, 95);
- }
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- return UIEdgeInsetsMake(0, 5, 0, 5);
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.row == dataArr.count) {
- LDSuperBrandMoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:moreCellId forIndexPath:indexPath];
- cell.backgroundColor = [UIColor whiteColor];
- return cell;
- }
- LDSuperBrandCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
- LDSuperBrandModel *model;
- if (dataArr.count > indexPath.row) {
- model = dataArr[indexPath.row];
- }
- cell.model = model;
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.row == dataArr.count) {
- if (self.moreClick) {
- self.moreClick();
- }
- }else {
- if (self.selectedClick) {
- LDSuperBrandModel *model;
- if (dataArr.count > indexPath.row) {
- model = dataArr[indexPath.row];
- }
- self.selectedClick(model);
- }
- }
- }
- @end
|