长按移动cell
2016-05-27 18:08
190 查看
- (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);
}
}
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);
}
}
相关文章推荐
- php编程安全指南
- 自定义之类微信底部渐变栏
- 解决部分手机发送get请求时传递中文参数服务器获取不到正确参数
- 万能的RecyclerView适配器(可添加头)
- gridview安卓实现单行多列横向滚动
- gridview安卓实现单行多列横向滚动
- Spring回滚事务机制与创建数组的语法,Object[] params = new Object[]{值}
- Spring RestTemplate详解
- 系统调用服务号 linux 2.6.32
- C#中的引用传递和值传递。
- Spring中的设计模式
- java面向对象_构造器
- C51单片机-串行口2-蓝牙模块-应用例程
- localhost:8080 自动变成了 IP 地址
- javascript 面向对象基础(三)面向对象的常用属性和方法
- Java综合项目分析
- HDU 1258 Sum It Up(DFS)
- 新浪 SDK编码规范
- 从病毒到AU3编程语言
- GitHub的使用