UICollectionView 拖拽重排

iOS9之后,UICollectionView使用API可实现拖拽重排,粘贴的工程中,通过push跳转到"CollectionVVVVV"可查看效果。

@interface CollectionVVVVV :UIViewController

@end

#import "CollectionVVVVV.h"
@interface CollectionViewCelll : UICollectionViewCell
@property (nonatomic, strong) UILabel *title;
@property (nonatomic , copy) NSString *titleStr;
@end
@implementation CollectionViewCelll
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *title = [[UILabel alloc] init];
        title.backgroundColor = [UIColor whiteColor];
        title.frame = self.contentView.bounds;
        [self.contentView addSubview:title];
        self.title = title;
    }
    return self;
}

-(void)cellTitleStr:(NSString *)titleStr{
    self.title.text = titleStr;
}
@end

@interface CollectionVVVVV ()
@property (nonatomic , strong) UICollectionView *collectionV;
@property (nonatomic , strong) NSMutableArray *dataArray;
@end
#define SCwidth  self.view.frame.size.width
@implementation CollectionVVVVV
-(UICollectionView *)collectionV{
    
    if (!_collectionV) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(SCwidth/4-15, SCwidth/4 -15);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.originaHeight, SCwidth, self.view.frame.size.height) collectionViewLayout:flowLayout];
        _collectionV.delegate = self;
        _collectionV.dataSource = self;
        //        _collectionV.backgroundColor = [UIColor grayColor];
        [_collectionV registerClass:[CollectionViewCelll class] forCellWithReuseIdentifier:@"cell"];
        _collectionV.backgroundColor = [UIColor orangeColor];
    }
    UILongPressGestureRecognizer *longp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPress:)];
    [_collectionV addGestureRecognizer:longp];
    return _collectionV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    for (int i = 0;  i <20 ; i ++) {
        NSString *title = [NSString stringWithFormat:@"南玻 %d",i];
        [self.dataArray addObject:title];
    }
    [self.view addSubview:self.collectionV];
    [self.collectionV reloadData];
    self.view.backgroundColor = [UIColor whiteColor];
}
-(void)LongPress:(UILongPressGestureRecognizer*)longp{
    CGPoint point = [longp locationInView:self.collectionV];
    NSIndexPath *index = [self.collectionV indexPathForItemAtPoint:point];
    CollectionViewCelll *cell = (CollectionViewCelll*)[self.collectionV cellForItemAtIndexPath:index];
    switch (longp.state) {
        case UIGestureRecognizerStateBegan:
        {
            [UIView animateWithDuration:0.2 animations:^{
                cell.transform = CGAffineTransformMakeScale(1.3, 1.3);
            } completion:^(BOOL finished) {
            }];
            
            if (!index) {
                break;
            }
            
            BOOL canMove = [self.collectionV beginInteractiveMovementForItemAtIndexPath:index];
            if (!canMove) {
                break;
            }
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            [self.collectionV updateInteractiveMovementTargetPosition:point];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [self.collectionV endInteractiveMovement];
        }
            break;
        default:
        {
            [self.collectionV cancelInteractiveMovement];
        }
            break;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSString *temp = [_dataArray objectAtIndex:sourceIndexPath.row];
    [_dataArray removeObjectAtIndex:sourceIndexPath.row];
    [_dataArray insertObject:temp atIndex:destinationIndexPath.row];
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCelll* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.title setText:_dataArray[indexPath.row]];   
    return cell;
}
@end

你可能感兴趣的:(UICollectionView 拖拽重排)