#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:@"第一个",@"第二个",@"第三个",@"第四个",@"第五个",@"第六个",@"第七个",@"第八个",@"第九个",@"第十个",nil];
}
return _list;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
// UICollectionViewFlowLayout流水布局的内部成员属性有以下:
/**
@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);
// 设置最小行间距
layout.minimumLineSpacing = 10;
// 设置垂直间距
layout.minimumInteritemSpacing = 10;
// 设置滚动方向(默认垂直滚动)
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 添加长按手势
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longHandle:)];
[collectionView addGestureRecognizer:longTap];
// Do any additional setup after loading the view.
}
//2.长按方法
-(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添加抖动手势
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移除抖动手势
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.设置可移动
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//4.移动完成后的方法 -- 交换数据
-(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)];//度数转弧度
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"];
}
iOS UICollectionView 长按移动item及抖动动画
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前段时间由于公司需求,恰好需要做一个CollectionView的Item长按后抖动并且可移动效果。但由于一...
- 点击每一个item后移动item改变item的位置。实现这个功能有两种方案1.第一种使用苹果给出的API直接调方法...
- 项目中用到了这个功能,感觉挺好玩的,所以就研究总结了一下,写了个demo大家一起review一下,哈哈哈。git下...
- iOS 9之后: 示例如下 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. 手残效果图没弄好....
- UICollectionView添加手势,可以使cell移动、添加、删除,这种功能网上也有一大堆的资料可供查看,本...