// // KWMNewGoodsCell.m // iCemarose // // Created by 陈荣科 on 16/9/1. // Copyright © 2016年 kollway. All rights reserved. // #import "KWMNewGoodsCell.h" #import "KWMCollectionCell.h" @implementation KWMNewGoodsCell{ NSInteger leftMargin; } static NSString *idStr = @"KWMCollectionCell"; - (void)awakeFromNib { [super awakeFromNib]; // Initialization code self.selectionStyle = UITableViewCellSelectionStyleNone; self.clvNewGoods.decelerationRate = UIScrollViewDecelerationRateFast; // 底部渐变 CAGradientLayer *gradientLy = [self shadowAsInverse:[UIColor redColor]]; _vWatch.layer.mask = gradientLy; [self initCollectionView]; } - (void)initCollectionView{ self.isSameBrand = NO; self.clvNewGoods.delegate = self; self.clvNewGoods.backgroundColor = [UIColor clearColor]; self.clvNewGoods.showsHorizontalScrollIndicator = NO; [self.clvNewGoods registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellWithReuseIdentifier:idStr]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; if (_isDetail) { leftMargin = 0; }else{ leftMargin = 20; } layout.minimumLineSpacing = 0; layout.minimumInteritemSpacing = 0; layout.sectionInset = UIEdgeInsetsMake(40, leftMargin, 35, 0); layout.itemSize = CGSizeMake(134, 180); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.clvNewGoods.collectionViewLayout = layout; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if(self.products && self.products.count>9){ return 10; } if(self.products && self.products.count>0){ return self.products.count; }else if(self.loadStatus && self.loadStatus.cacheData){ return self.loadStatus.cacheData.count; }else { return 5; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ KWMCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:idStr forIndexPath:indexPath]; cell.isMore = indexPath.row == 9; if(self.products && self.products.count>indexPath.row){ [cell setData:[self.products objectAtIndex:indexPath.row]]; }else if(self.loadStatus && self.loadStatus.cacheData && self.loadStatus.cacheData.count>indexPath.row){ [cell setData:[self.loadStatus.cacheData objectAtIndex:indexPath.row]]; }else{ [cell setData:nil]; } //如果滑动了后面10个,尝试获取下一页 if(self.products!=nil && indexPath.row == self.products.count-10 && self.delegate!=nil){ if ([self.delegate respondsToSelector:@selector(kwm_onNexPage:)]) { [self.delegate kwm_onNexPage:self.isSameBrand]; } } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row == 9){ if ([self.delegate respondsToSelector:@selector(kwm_onClickMore:)]) { [self.delegate kwm_onClickMore:self.isSameBrand]; } return; } BUYProduct *selectProduct; if(self.products!=nil && self.products.count>indexPath.row){ selectProduct = [self.products objectAtIndex:indexPath.row]; }else if(self.loadStatus && self.loadStatus.cacheData && self.loadStatus.cacheData.count>indexPath.row){ selectProduct = [self.loadStatus.cacheData objectAtIndex:indexPath.row]; } if ([self.delegate respondsToSelector:@selector(kwm_selectCollectionCell:)]) { [self.delegate kwm_selectCollectionCell:selectProduct]; } NSLog(@"row = %ld",(long)indexPath.row); } - (NSMutableArray *)products { if(_products == nil){ _products = [[NSMutableArray alloc] initWithCapacity:100]; } return _products; } -(void)setLoadStatusData:(KWMLoadStatus *) loadStatus{ if(loadStatus){ self.loadStatus = loadStatus; [self setData:loadStatus.data]; if(loadStatus.scrollOffset == 0){ //135为cell的宽度 NSInteger screen = UI_SCREEN_WIDTH; NSInteger scrollIndex = (screen-20) % 135; if(scrollIndex<35){ self.loadStatus.scrollOffset = 135 * 0.2 - scrollIndex; }else if(scrollIndex>90){ self.loadStatus.scrollOffset = 135 * 1.2 - scrollIndex; } } [self.clvNewGoods layoutIfNeeded]; [self.clvNewGoods setContentOffset:CGPointMake(loadStatus.scrollOffset, 0) animated:NO]; }else{ [self setData:nil]; } } -(void)setData:(NSArray *)products{ [self.products removeAllObjects]; if(products){ [self addData:products]; } [self.clvNewGoods reloadData]; } -(void)addData:(NSArray *)products{ if(products == nil){ return; } [self.products addObjectsFromArray:products]; [self.clvNewGoods reloadData]; } -(void)scrollToTop{ [self.clvNewGoods setContentOffset:CGPointMake(0, 0) animated:NO]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ //self.vWatch.hidden = NO; self.vWatch.alpha = 1; //底部渐变 CAGradientLayer *gradientLy = [self shadowAsInverse:[UIColor redColor]]; _vWatch.layer.mask = gradientLy; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ //self.vWatch.alpha = 0; } - (CAGradientLayer *)shadowAsInverse:(UIColor *)color{ CAGradientLayer *newShadow = [[CAGradientLayer alloc] init]; CGRect newShadowFrame = CGRectMake(0, 0, 25, 180); newShadow.frame = newShadowFrame; newShadow.startPoint = CGPointMake(0, 0); newShadow.endPoint = CGPointMake(1, 0); //添加渐变的颜色组合(颜色透明度的改变) newShadow.colors = [NSArray arrayWithObjects: (id)[[color colorWithAlphaComponent:0.0] CGColor] , (id)[[color colorWithAlphaComponent:0.8] CGColor], nil]; return newShadow; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ // 实时监测scrollView.contentInset.top, 系统优化以及手动设置contentInset都会影响contentInset.top。 if(self.loadStatus){ self.loadStatus.scrollOffset = scrollView.contentOffset.x; } } @end