-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCellBox.m
More file actions
executable file
·289 lines (268 loc) · 11.8 KB
/
CellBox.m
File metadata and controls
executable file
·289 lines (268 loc) · 11.8 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// CellBox.m
// CellBox_OC
//
// Created by Jx on 15/7/9.
// Copyright © 2015年 Jx. All rights reserved.
/*
Example:[[[CellBox alloc]init:2]show];
*/
#import "CellBox.h"
#import "LineLayout.h"
#import "Cell.h"
#define EdgeInsets 24.0
@implementation CellBox
- (instancetype)init: (CGRect)cvcFrame numOfCell: (int) cellsNum{
cellNum = cellsNum;
cellNow = 1;
cellSize = CGSizeMake(cvcFrame.size.width - EdgeInsets * 2, cvcFrame.size.height - 20);
collectionViewFrame = cvcFrame;
minimumLineSpacing = 12;
mainCellLineSpacing = 24;
LineLayout *lineLayout = [[LineLayout alloc] init:cellSize withMinimumLineSpacing:12 andMainCellLineSpacing:24];
self = [super initWithCollectionViewLayout:lineLayout];
return self;
}
-(void)viewDidLoad{
self.collectionView.decelerationRate = 0.1;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.showsVerticalScrollIndicator = NO;
self.collectionView.backgroundColor = [UIColor clearColor];
cellID = [_cellDataSourcedDelegate registerCellClassForCllectionView:self.collectionView];
isThouch = NO;
stateOfSlide = NOT_SLIDE;
if (cellNum == 1) {
[self.collectionView setScrollEnabled:NO];
}
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell;
if (cellNow == 1 && cellNow != cellNum) {
switch (indexPath.item) {
case 0:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNum];
}
break;
case 1:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNum];
}
break;
case 2:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:1];
}
break;
case 3:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:2];
}
break;
case 4:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:2];
}
break;
default:
break;
}
}else if(cellNow == cellNum){
switch (indexPath.item) {
case 0:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow - 1];
}
break;
case 1:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow - 1];
}
break;
case 2:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow];
}
break;
case 3:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:1];
}
break;
case 4:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:1];
}
break;
default:
break;
}
}else{
switch (indexPath.item) {
case 0:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow - 1];
}
break;
case 1:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow - 1];
}
break;
case 2:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow];
}
break;
case 3:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow + 1];
}
break;
case 4:
if (_cellDataSourcedDelegate) {
cell = [_cellDataSourcedDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath cellWithIdentifier:cellID cellWantsIndex:cellNow + 1];
}
break;
default:
break;
}
}
if (cellNum == 1) {
if (indexPath.item != 2) {
cell.hidden = YES;
}
}
return cell;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
-(void)show:(UIViewController *)lastViewController{
// blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
// blurView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
// blurView.alpha = 0.8;
// blurView.frame = CGRectMake(0, 20, lastViewController.view.bounds.size.width, lastViewController.view.bounds.size.height);
self.view.frame = collectionViewFrame;
// blurView.frame = collectionViewFrame;
// [lastViewController.view addSubview:blurView];
[lastViewController.view addSubview:self.view];
[lastViewController addChildViewController:self];
}
- (void)close{
//[self.collectionView removeFromSuperview];
[blurView removeFromSuperview];
[self.view removeFromSuperview];
[self removeFromParentViewController];
@try {
[_cellDataSourcedDelegate collectionViewColsed];
}
@catch (NSException *exception) {
NSLog(@"Delegate not work able");
}
}
- (UIImage *)makeAImage:(UIColor *)color size:(CGRect)rect{
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[self.collectionView setScrollEnabled:YES];
if (_cellDataSourcedDelegate) {
[_cellDataSourcedDelegate collectionView:self didSelectItemAtIndexPath:indexPath];
}
if (cellNum == 1) {
[self.collectionView setScrollEnabled:NO];
}
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
isThouch = YES;
scrollView.decelerationRate = 0.1;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (decelerate && isThouch)
{
dispatch_async(dispatch_get_main_queue(), ^{
printf("STOP IT!!\n");
[scrollView setContentOffset:scrollView.contentOffset animated:NO];
});
isThouch = NO;
CGPoint offset = [self targetContentOffsetForProposedContentOffset:self.collectionView.contentOffset];
NSLog(@"%f,%f",offset.x,self.collectionView.contentOffset.x);
dispatch_async(dispatch_get_main_queue(), ^{
printf("STOP IT!!\n");
///////////////
[scrollView setContentOffset:offset animated:YES];
});
}else if (decelerate){
}
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[self reloadCells];
if (self.cellDataSourcedDelegate && stateOfSlide != NOT_SLIDE) {
@try {
if (stateOfSlide == SLIDE_LEFT) {
[self.cellDataSourcedDelegate collectionView:self didSlideAtItem:[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]]];
}else{
[self.cellDataSourcedDelegate collectionView:self didSlideAtItem:[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:0]]];
}
}
@catch (NSException *exception) {
}
}
}
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset{
CGPoint pointAfterSlide;
CGFloat rightSlideThreshold = 10 + cellSize.width * 2 + minimumLineSpacing * 2 - mainCellLineSpacing + cellSize.width / 4;
CGFloat leftSlideThreshold = 10 + cellSize.width + minimumLineSpacing + cellSize.width + minimumLineSpacing - cellSize.width / 4;
if (proposedContentOffset.x >= rightSlideThreshold) {
stateOfSlide = SLIDE_RIGHT;
if (cellNow != cellNum) {
cellNow++;
}else{
cellNow = 1;
}
pointAfterSlide = CGPointMake(10 + cellSize.width * 3 + minimumLineSpacing * 3 - mainCellLineSpacing, proposedContentOffset.y);
}else{
if (proposedContentOffset.x <= leftSlideThreshold) {
stateOfSlide = SLIDE_LEFT;
if (cellNow != 1) {
cellNow--;
}else{
cellNow = cellNum;
}
pointAfterSlide = CGPointMake(10 + cellSize.width + minimumLineSpacing - mainCellLineSpacing, proposedContentOffset.y);
}else{
pointAfterSlide = CGPointMake(10 + cellSize.width * 2 + minimumLineSpacing * 2 - mainCellLineSpacing, proposedContentOffset.y);
stateOfSlide = NOT_SLIDE;
}
}
return pointAfterSlide;
}
- (void)reloadCells{
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
[self.collectionView setContentOffset: CGPointMake(10 + (collectionViewFrame.size.width - 48) * 2 + minimumLineSpacing * 2 - mainCellLineSpacing, 0) animated:NO];
});
}
@end