我有 UICollectionView 我在其上显示 定制 细胞。我试图通过长按手势来移动细胞,但它根本不起作用。
我关注了this教程,但它对我不起作用。请告诉我该怎么做?
长按手势
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
NSLog(@"long press geture called");
switch(gestureRecognizer.state)
{
case UIGestureRecognizerStateBegan:
p = [gestureRecognizer locationInView:self.collection_view];
gesture_indexPath = [self.collection_view indexPathForItemAtPoint:p];
[self.collection_view beginInteractiveMovementForItemAtIndexPath:gesture_indexPath];
NSLog(@"state began called");
break;
case UIGestureRecognizerStateChanged:
// collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
[self.collection_view updateInteractiveMovementTargetPosition:[gestureRecognizer locationInView:[gestureRecognizer view]]];
NSLog(@"state changed called");
case UIGestureRecognizerStateEnded:
[self.collection_view updateInteractiveMovementTargetPosition:p];
NSLog(@"state changed called");
default:
[self.collection_view cancelInteractiveMovement];
}
}
方法覆盖
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSLog(@"Move at index path called");
}
最佳答案
你错过了break;
switch-case block 中的语句。没有 break;
在每个 case 部分的末尾,不退出 switch-case 语句,而是执行下一行。
为了完整起见,这里是 Objective-C 中用于可重新排序的 UICollectionView
的最低实现。 .
为 UICollectionView
实现重新排序不是 嵌入到 UICollectionViewController
(可能是在 UIViewController
内,但也可能在 UICollectionViewCell
或 UITableViewCell
内,如果你喜欢的话)至少需要以下三个部分:
viewDidLoad
(或者,如果您将 UICollectionView
嵌入到 UITableViewCell
或 UICollectionViewCell
在 Storyboard 中设置,则此代码将进入 awakeFromNib
),添加 UILongPressGestureRecognizer
给您的UICollectionView
像这样:UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)
[self.yourCollectionView addGestureRecognizer:longGR];
handleLongPress:
对 UILongPressGestureRecognizer
使用react您刚刚添加:- (void)handleLongPress:(UILongPressGestureRecognizer *)gr{
switch(gr.state){
case UIGestureRecognizerStateBegan:
{
NSIndexPath *selectedIndexPath = [self.yourCollectionView indexPathForItemAtPoint:[gr locationInView:self.yourCollectionView]];
if(selectedIndexPath == nil) break;
[self.yourCollectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
break;
}
case UIGestureRecognizerStateChanged:
{
[self.yourCollectionView updateInteractiveMovementTargetPosition:[gr locationInView:gr.view]];
break;
}
case UIGestureRecognizerStateEnded:
{
[self.yourCollectionView endInteractiveMovement];
break;
}
default:
{
[self.yourCollectionView cancelInteractiveMovement];
break;
}
}
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//add your data source manipulation logic here
//specifically, change the order of entries in the data source to match the new visual order of the cells.
//even without anything inside this function, the cells will move visually if you build and run
}
....当您触摸、按住、拖动时,细胞现在会神奇地移动!
(这一切都假设您已经实现了标准的
UICollectionView
数据源和委托(delegate)方法,并且 UICollectionView
已成功加载/显示在您的应用程序中。如果您需要有关 UICollectionView
的基本设置的帮助,请查看this post 作者:狼人)。
关于ios - 无法通过移动它们来重新排列 UICollectionView 中的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33604209/