移動UICollectionViewCell
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
//判斷手勢狀態
switch (longGesture.state) {
case UIGestureRecognizerStateBegan:{
//判斷手勢落點位置是否在路徑上
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
if (indexPath == nil) {
break;
}
//在路徑上則開始移動該路徑上的cell
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
case UIGestureRecognizerStateChanged:
//移動過程當中隨時更新cell位置
[self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
break;
case UIGestureRecognizerStateEnded:
//移動結束後關閉cell移動
[self.collectionView endInteractiveMovement];
break;
default:
[self.collectionView cancelInteractiveMovement];
break;
}
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
//返回YES允許其item移動
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
//取出源item數據
id objc = [_dataSource objectAtIndex:sourceIndexPath.item];
//從資源數組中移除該數據
[_dataSource removeObject:objc];
//將數據插入到資源數組中的目標位置上
//判斷手勢狀態
switch (longGesture.state) {
case UIGestureRecognizerStateBegan:{
//判斷手勢落點位置是否在路徑上
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
if (indexPath == nil) {
break;
}
//在路徑上則開始移動該路徑上的cell
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
case UIGestureRecognizerStateChanged:
//移動過程當中隨時更新cell位置
[self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
break;
case UIGestureRecognizerStateEnded:
//移動結束後關閉cell移動
[self.collectionView endInteractiveMovement];
break;
default:
[self.collectionView cancelInteractiveMovement];
break;
}
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
//返回YES允許其item移動
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
//取出源item數據
id objc = [_dataSource objectAtIndex:sourceIndexPath.item];
//從資源數組中移除該數據
[_dataSource removeObject:objc];
//將數據插入到資源數組中的目標位置上
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.