效果圖.gif
#import "CollectionMoveViewController.h"
#import "CollectionViewCell.h"
@interface CollectionMoveViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *list;
@end
@implementation CollectionMoveViewController
-(NSMutableArray *)list
{
if (_list == nil) {
_list = [NSMutableArray arrayWithObjects:@"第一個(gè)",@"第二個(gè)",@"第三個(gè)",@"第四個(gè)",@"第五個(gè)",@"第六個(gè)",@"第七個(gè)",@"第八個(gè)",@"第九個(gè)",@"第十個(gè)",nil];
}
return _list;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
// UICollectionViewFlowLayout流水布局的內(nèi)部成員屬性有以下:
/**
@property (nonatomic) CGFloat minimumLineSpacing;
@property (nonatomic) CGFloat minimumInteritemSpacing;
@property (nonatomic) CGSize itemSize;
@property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -preferredLayoutAttributesFittingAttributes:
@property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
@property (nonatomic) CGSize headerReferenceSize;
@property (nonatomic) CGSize footerReferenceSize;
@property (nonatomic) UIEdgeInsets sectionInset;
*/
// 定義大小
layout.itemSize = CGSizeMake(100, 100);
// 設(shè)置最小行間距
layout.minimumLineSpacing = 10;
// 設(shè)置垂直間距
layout.minimumInteritemSpacing = 10;
// 設(shè)置滾動(dòng)方向(默認(rèn)垂直滾動(dòng))
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DZHWidth, DZHHeight) collectionViewLayout:layout];
self.collectionView = collectionView;
[self.view addSubview:collectionView];
self.collectionView.backgroundColor = [UIColor greenColor];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
collectionView.pagingEnabled = YES;
//1.CollectionView 添加長(zhǎng)按手勢(shì)
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longHandle:)];
[collectionView addGestureRecognizer:longTap];
// Do any additional setup after loading the view.
}
//2.長(zhǎng)按方法
-(void)longHandle:(UILongPressGestureRecognizer *)gesture
{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
if (indexPath == nil) {
break;
}
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
//cell.layer添加抖動(dòng)手勢(shì)
for (CollectionViewCell *cell in [self.collectionView visibleCells]) {
[self starShake:cell];
}
break;
}
case UIGestureRecognizerStateChanged:
{
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:self.collectionView]];
break;
}
case UIGestureRecognizerStateEnded:
{
[self.collectionView endInteractiveMovement];
//cell.layer移除抖動(dòng)手勢(shì)
for (CollectionViewCell *cell in [self.collectionView visibleCells]) {
[self stopShake:cell];
}
break;
}
default:
[self.collectionView cancelInteractiveMovement];
break;
}
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.list.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
cell.textLabel.text = self.list[indexPath.row];
return cell;
}
//3.設(shè)置可移動(dòng)
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//4.移動(dòng)完成后的方法 -- 交換數(shù)據(jù)
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[self.list exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
- (void)starShake:(CollectionViewCell*)cell{
CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
keyAnimaion.keyPath = @"transform.rotation";
keyAnimaion.values = @[@(-3 / 180.0 * M_PI),@(3 /180.0 * M_PI),@(-3/ 180.0 * M_PI)];//度數(shù)轉(zhuǎn)弧度
keyAnimaion.removedOnCompletion = NO;
keyAnimaion.fillMode = kCAFillModeForwards;
keyAnimaion.duration = 0.3;
keyAnimaion.repeatCount = MAXFLOAT;
[cell.layer addAnimation:keyAnimaion forKey:@"cellShake"];
}
- (void)stopShake:(CollectionViewCell*)cell{
[cell.layer removeAnimationForKey:@"cellShake"];
}