1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// 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