您的位置:首页 > 其它

长按移动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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: