-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewController.m
314 lines (268 loc) · 13.1 KB
/
ViewController.m
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// ViewController.m
// CollectionviewSort
//
// Created by wangwenke on 16/4/12.
// Copyright © 2016年 wangwenke. All rights reserved.
//
#import "ViewController.h"
#import "MineCollectionViewCell.h"
#import "CollectionReusableView.h"
#define DEVICE_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define DEVICE_HEIGHT ([UIScreen mainScreen].bounds.size.height)
static NSString *collectionCell = @"mineCell";
static NSString *collectionHeader = @"mineHeader";
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *mineCollection;
@property (nonatomic, strong) NSMutableArray *imagesSectionOneArray;
@property (nonatomic, strong) NSMutableArray *imagesSectionTwoArray;
@property (nonatomic, strong) NSMutableArray *cellAttributesArray;
@property (nonatomic, assign) CGPoint lastPressPoint;
/**
* scrollerTimer
*/
@property (nonatomic, strong) NSTimer *scrollerTimer;
@property (nonatomic, assign) CGFloat scrollerValue;
@property (nonatomic, assign) BOOL isCanSort;//是否支持排序功能
//用于判断一、二分区是否有移动动画
@property (nonatomic, assign) BOOL isSorting;
@property (nonatomic, assign) BOOL sectionOneIsSort;
@property (nonatomic, assign) BOOL sectionTwoIsSort;
@end
@implementation ViewController
- (NSMutableArray *)imagesSectionOneArray{
if (!_imagesSectionOneArray) {
self.imagesSectionOneArray = [NSMutableArray arrayWithCapacity:0];
}
return _imagesSectionOneArray;
}
- (NSMutableArray *)imagesSectionTwoArray{
if (!_imagesSectionTwoArray) {
self.imagesSectionTwoArray = [NSMutableArray arrayWithCapacity:0];
}
return _imagesSectionTwoArray;
}
- (NSMutableArray *)cellAttributesArray{
if (!_cellAttributesArray) {
self.cellAttributesArray = [NSMutableArray arrayWithCapacity:0];
}
return _cellAttributesArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.automaticallyAdjustsScrollViewInsets = NO;
_lastPressPoint = CGPointZero;
_isCanSort = YES;
self.view.backgroundColor = [UIColor lightGrayColor];
for (int i = 0; i < 15; i++) {
[self.imagesSectionOneArray addObject:[NSString stringWithFormat:@"%d",i % 10 + 1]];
}
for (int i = 21; i < 30; i++) {
[self.imagesSectionTwoArray addObject:[NSString stringWithFormat:@"%d",i]];
}
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing = 5.0;
layout.minimumInteritemSpacing = 5.0;
layout.sectionInset = UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
_mineCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64.0, DEVICE_WIDTH, DEVICE_HEIGHT - 64.0) collectionViewLayout:layout];
_mineCollection.backgroundColor = [UIColor lightGrayColor];
_mineCollection.dataSource = self;
_mineCollection.delegate = self;
[_mineCollection registerClass:[MineCollectionViewCell class] forCellWithReuseIdentifier:collectionCell];
[_mineCollection registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionHeader];
[self.view addSubview:_mineCollection];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 0) {
return self.imagesSectionOneArray.count;
}
return self.imagesSectionTwoArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake((DEVICE_WIDTH - 25.0) / 4.0, (DEVICE_WIDTH - 25.0) / 4.0);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(DEVICE_WIDTH, 44.0);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCell forIndexPath:indexPath];
cell.hidden = NO;
cell.backgroundColor = [UIColor whiteColor];
if (indexPath.section == 0) {
cell.cellImage.image = [UIImage imageNamed:self.imagesSectionOneArray[indexPath.row]];
if (_sectionOneIsSort && indexPath.row + 1 == self.imagesSectionOneArray.count) {
cell.hidden = YES;
}
}else{
cell.cellImage.image = [UIImage imageNamed:self.imagesSectionTwoArray[indexPath.row]];
if (_sectionTwoIsSort && indexPath.row + 1 == self.imagesSectionTwoArray.count) {
cell.hidden = YES;
}
}
for (UIGestureRecognizer *gesture in cell.gestureRecognizers) {
if ([gesture isKindOfClass:[UILongPressGestureRecognizer class]]) {
[cell removeGestureRecognizer:gesture];
}
}
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
[cell addGestureRecognizer:longPress];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
CollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:collectionHeader forIndexPath:indexPath];
reusableView.backgroundColor = [UIColor whiteColor];
if (indexPath.section == 0) {
reusableView.title.text = [NSString stringWithFormat:@"section one"];
}else{
reusableView.title.text = [NSString stringWithFormat:@"section two"];
}
return reusableView;
}
return nil;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (_isCanSort) {
[self startMoveClickedCellAtIndexpath:indexPath];
return;
}
}
//长按拖动排序
- (void)longPressGesture:(UILongPressGestureRecognizer *)sender{
if (!_isCanSort) {
return;
}
MineCollectionViewCell *cell = (MineCollectionViewCell *)sender.view;
[_mineCollection bringSubviewToFront:cell];
NSIndexPath *cellIndexPath = [_mineCollection indexPathForCell:cell];
[_mineCollection bringSubviewToFront:cell];
BOOL isChanged = NO;
if (sender.state == UIGestureRecognizerStateBegan) {
self.lastPressPoint = [sender locationInView:_mineCollection];
}else if (sender.state == UIGestureRecognizerStateChanged){
[self.cellAttributesArray removeAllObjects];
for (int i = 0;i < self.imagesSectionOneArray.count; i++) {
[self.cellAttributesArray addObject:[_mineCollection layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]];
}
for (int i = 0;i < self.imagesSectionTwoArray.count; i++) {
[self.cellAttributesArray addObject:[_mineCollection layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:1]]];
}
[self scrollerCollectionView:[sender locationInView:self.view]];
cell.center = [sender locationInView:_mineCollection];
for (UICollectionViewLayoutAttributes *attributes in self.cellAttributesArray) {
if (CGRectContainsPoint(attributes.frame, cell.center) && cellIndexPath != attributes.indexPath) {
isChanged = YES;
//对数组中存放的元素重新排序
if (cellIndexPath.section == attributes.indexPath.section) {
if (cellIndexPath.section == 0) {
NSString *imageStr = self.imagesSectionOneArray[cellIndexPath.row];
[self.imagesSectionOneArray removeObjectAtIndex:cellIndexPath.row];
[self.imagesSectionOneArray insertObject:imageStr atIndex:attributes.indexPath.row];
}else{
NSString *imageStr = self.imagesSectionTwoArray[cellIndexPath.row];
[self.imagesSectionTwoArray removeObjectAtIndex:cellIndexPath.row];
[self.imagesSectionTwoArray insertObject:imageStr atIndex:attributes.indexPath.row];
}
[self.mineCollection moveItemAtIndexPath:cellIndexPath toIndexPath:attributes.indexPath];
}else{
if (cellIndexPath.section == 0) {//一区移动到二区
NSString *imageStr = self.imagesSectionOneArray[cellIndexPath.row];
[self.imagesSectionOneArray removeObjectAtIndex:cellIndexPath.row];
[self.imagesSectionTwoArray insertObject:imageStr atIndex:attributes.indexPath.row];
}else{//二区移动到一区
NSString *imageStr = self.imagesSectionTwoArray[cellIndexPath.row];
[self.imagesSectionTwoArray removeObjectAtIndex:cellIndexPath.row];
[self.imagesSectionOneArray insertObject:imageStr atIndex:attributes.indexPath.row];
}
[self.mineCollection moveItemAtIndexPath:cellIndexPath toIndexPath:attributes.indexPath];
}
}
}
}else if (sender.state == UIGestureRecognizerStateEnded) {
if (!isChanged) {
cell.center = [_mineCollection layoutAttributesForItemAtIndexPath:cellIndexPath].center;
}
NSLog(@"排序后---%d--%d",self.imagesSectionOneArray.count,self.imagesSectionTwoArray.count);
}
}
//点击时排序
- (void)startMoveClickedCellAtIndexpath:(NSIndexPath *)indexPath{
if (_isSorting) {
return;
}
_isSorting = YES;
MineCollectionViewCell *movedCell = (MineCollectionViewCell *)[_mineCollection cellForItemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes *endAttributes = nil;
NSIndexPath *endIndexPath = nil;
if (indexPath.section == 0) {
_sectionOneIsSort = NO;
_sectionTwoIsSort = YES;
[self.imagesSectionTwoArray addObject:[self.imagesSectionOneArray objectAtIndex:indexPath.row]];
endIndexPath = [NSIndexPath indexPathForItem:_imagesSectionTwoArray.count - 1 inSection:1];
[self.mineCollection insertItemsAtIndexPaths:@[endIndexPath]];
[self.mineCollection reloadSections:[NSIndexSet indexSetWithIndex:1]];
}else{
_sectionOneIsSort = YES;
_sectionTwoIsSort = NO;
[self.imagesSectionOneArray addObject:[self.imagesSectionTwoArray objectAtIndex:indexPath.row]];
endIndexPath = [NSIndexPath indexPathForItem:_imagesSectionOneArray.count - 1 inSection:0];
[self.mineCollection insertItemsAtIndexPaths:@[endIndexPath]];
[self.mineCollection reloadSections:[NSIndexSet indexSetWithIndex:0]];
}
endAttributes = [_mineCollection layoutAttributesForItemAtIndexPath:endIndexPath];
MineCollectionViewCell __weak *endCell = (MineCollectionViewCell *)[_mineCollection cellForItemAtIndexPath:endIndexPath];
typeof(self) __weak weakSelf = self;
[UIView animateWithDuration:0.3 animations:^{
movedCell.center = endAttributes.center;
} completion:^(BOOL finished) {
endCell.hidden = NO;
movedCell.hidden = YES;
weakSelf.sectionOneIsSort = NO;
weakSelf.sectionTwoIsSort = NO;
if (indexPath.section == 0) {
[weakSelf.imagesSectionOneArray removeObjectAtIndex:indexPath.row];
[weakSelf.mineCollection deleteItemsAtIndexPaths:@[indexPath]];
}else{
[weakSelf.imagesSectionTwoArray removeObjectAtIndex:indexPath.row];
[weakSelf.mineCollection deleteItemsAtIndexPaths:@[indexPath]];
}
weakSelf.isSorting = NO;
}];
}
//自动滑动collectionView
- (void)scrollerCollectionView:(CGPoint)point{
if (point.y <= 10 + 64.0) {
_scrollerValue = -1.0;
}else if (point.y >= DEVICE_HEIGHT - 20.0){
_scrollerValue = 1.0;
}else{
if (_scrollerTimer) {
[_scrollerTimer invalidate];
_scrollerTimer = nil;
}
return;
}
if (!_scrollerTimer) {
_scrollerTimer = [NSTimer scheduledTimerWithTimeInterval:0.007 target:self selector:@selector(startScrollerCollectionView) userInfo:nil repeats:YES];
[_scrollerTimer setFireDate:[NSDate distantPast]];
}
}
- (void)startScrollerCollectionView{
CGPoint point = self.mineCollection.contentOffset;
if (point.y + _scrollerValue <= 0 || point.y + _scrollerValue + self.mineCollection.bounds.size.height >= self.mineCollection.contentSize.height) {
[_scrollerTimer invalidate];
_scrollerTimer = nil;
return;
}
point = CGPointMake(point.x, point.y + _scrollerValue);
_mineCollection.contentOffset = point;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end