对于九宫格移动的问题,其主要就是在collectionView 上面添加一个长按手势,在实现长按手势的代码里面 主要有3个状态, 一个是长按手势的开启UIGestureRecognizerStateBegan UIGestureRecognizerStateChanged UIGestureRecognizerStateEnded这三种状态, 把握好在不同的状态下他们的趋势
#import "ViewController.h"
#import "MineCollectionViewCell.h"
#define DEVICE_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define DEVICE_HEIGHT ([UIScreen mainScreen].bounds.size.height)
static NSString *collectionCell = @"mineCell";
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *mineCollection;
@property (nonatomic, strong) NSMutableArray *imagesArray;
@property (nonatomic, strong) NSMutableArray *cellAttributesArray;
@property (nonatomic, assign) CGPoint lastPressPoint;
@end
@implementation ViewController
- (NSMutableArray *)imagesArray{
if (!_imagesArray) {
self.imagesArray = [NSMutableArray arrayWithCapacity:0];
}
return _imagesArray;
}
- (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;
self.view.backgroundColor = [UIColor lightGrayColor];
for (int i = 0; i < 10; i++) {
[self.imagesArray addObject:[NSString stringWithFormat:@"%d",i+1]];
}
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];
[self.view addSubview:_mineCollection];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.imagesArray.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);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCell forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.cellImage.image = [UIImage imageNamed:self.imagesArray[indexPath.row]];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
[cell addGestureRecognizer:longPress];
return cell;
}
- (void)longPressGesture:(UILongPressGestureRecognizer *)sender{
MineCollectionViewCell *cell = (MineCollectionViewCell *)sender.view;
NSIndexPath *cellIndexPath = [_mineCollection indexPathForCell:cell];
[_mineCollection bringSubviewToFront:cell];
BOOL isChanged = NO;
if (sender.state == UIGestureRecognizerStateBegan) {
[self.cellAttributesArray removeAllObjects];
for (int i = 0;i< self.imagesArray.count; i++) {
[self.cellAttributesArray addObject:[_mineCollection layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]];
}
self.lastPressPoint = [sender locationInView:_mineCollection];
}else if (sender.state == UIGestureRecognizerStateChanged){
cell.center = [sender locationInView:_mineCollection];
for (UICollectionViewLayoutAttributes *attributes in self.cellAttributesArray) {
if (CGRectContainsPoint(attributes.frame, cell.center) && cellIndexPath != attributes.indexPath) {
isChanged = YES;
//对数组中存放的元素重新排序
NSString *imageStr = self.imagesArray[cellIndexPath.row];
[self.imagesArray removeObjectAtIndex:cellIndexPath.row];
[self.imagesArray 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(@"排序后---%@",self.imagesArray);
}
}