#import "ViewController.h"
static NSString *identify = @"UICollectionViewIdentify";
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end
@implementation ViewController
-(NSMutableArray *)dataArray
{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
-(void)addMsg
{
for(NSInteger index = 0; index < 20; index ++)
{
NSString * string = [NSString stringWithFormat:@"%ld _ %ld", index, index * index];
[self.dataArray addObject:string];
}
}
-(UICollectionView *)collection
{
if (!_collection) {
//确认网格视图滚动的方向
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
_collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
_collection.delegate = self;
_collection.dataSource = self;
_collection.userInteractionEnabled = YES;
_collection.backgroundColor = [UIColor clearColor];
[_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identify];
//添加一个长按手势,用来移动cell
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveItemHereToThere:)];
[_collection addGestureRecognizer:longPress];
}
return _collection;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addMsg];
[self.view addSubview:self.collection];
}
#pragma mark - UICollectionViewDataSource
//展示cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataArray.count;
}
//展示的组数
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row * (indexPath.section + 1)) / 255.0) green:((20 * indexPath.row * (indexPath.section + 1))/255.0) blue:((30 * indexPath.row * (indexPath.section + 1))/255.0) alpha:1.0f];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 20)];
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = self.dataArray[indexPath.item];
for(id subView in cell.contentView.subviews)
{
[subView removeFromSuperview];
}
[cell.contentView addSubview:label];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//定义每个cell Item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat width = (collectionView.frame.size.width - 30)/ 3;
return CGSizeMake(width, width);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
#pragma mark - UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选中第 %ld组 _第%ld 行", indexPath.section, indexPath.item);
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//是否可以移动cell
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//取出移动目标的item信息objc
id objc = self.dataArray[sourceIndexPath.item];
//把取出的objc从数组里面删除
[self.dataArray removeObject:objc];
//再把objc插入到目标移动的位置
[self.dataArray insertObject:objc atIndex:destinationIndexPath.item];
}
//手势事件
-(void)moveItemHereToThere:(UILongPressGestureRecognizer *)longPress
{
//判断手势状态
switch (longPress.state) {
case UIGestureRecognizerStateBegan:
{
//取出路径并判断该路径是否存在
NSIndexPath *tempIndex = [self.collection indexPathForItemAtPoint:[longPress locationInView:self.collection]];
if (tempIndex == nil) {
break;
}
//移动cell
[self.collection beginInteractiveMovementForItemAtIndexPath:tempIndex];
}
break;
case UIGestureRecognizerStateChanged:
{
//移动cell的过程中随时的更新其他cell的状态
[self.collection updateInteractiveMovementTargetPosition:[longPress locationInView:self.collection]];
}
break;
case UIGestureRecognizerStateEnded:
{
//移动cell结束后关闭移动状态
[self.collection endInteractiveMovement];
}
break;
default:
{
//移动cell过程中取消
[self.collection cancelInteractiveMovement];
}
break;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在论坛上看到有这样一个demo,不过是用N个button实现的,后来自己觉得麻烦,则用网格视图写了个类似的功能。
这个写完运行,出现了卡顿现象,虽然功能实现,不流畅,后查询网上资料,用其介绍的方法完美解决。
参考资料:http://developer.51cto.com/art/201510/494049.htm