在所有UICollectionViewCells上应用手势

时间:2013-12-25 02:39:02

标签: ios objective-c uicollectionview

在我的应用程序中,我有一个UICollectionView。我想申请它的细胞一个长期压力的姿态。我实现了这一点,但是当我运行应用程序时,只有最后一个单元格工作而其他单元格没有响应。怎么了?这是我的代码。

    @interface
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressure;

在视图中加载:

self.longPressure = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePressue:)];

self.longPressure.delegate = self;

[self.collectionView addGestureRecognizer:self.longPressure];

手势处理程序:

- (void)handlePressue:(UILongPressGestureRecognizer*)gesture{

    [[gesture.view viewWithTag:1] setBackgroundColor:[UIColor yellowColor]];
}

in collectionView:cellforitemAtIndexPAth:

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
    imageView.tag = 1;
    [imageView setBackgroundColor:[UIColor redColor]];
    [cell addGestureRecognizer:self.longPressure];
    [cell addSubview:imageView];
    return cell;

有什么不对吗?

5 个答案:

答案 0 :(得分:0)

您需要在每个单元格上添加手势而不是UICollection视图。这可能是你的问题。最后一个单元格有效,因为手势只被添加到最后一个单元格中。

答案 1 :(得分:0)

您需要为每个单元格创建长按手势。因为手势像其他ui控件一样,只有一个在项目中。如果你想将它添加到其他视图,你需要复制另一个。

答案 2 :(得分:0)

查看以下代码段

MyFlowLayout *myLayout = [[MyFlowLayout alloc]init];
[self.collectionView setCollectionViewLayout:myLayout animated:YES];
UIGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
}
initWithTarget:self action:@selector(handlePinch:)];
[self.collectionView addGestureRecognizer:pinchRecognizer];

处理捏:

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)sender {
    // Get a reference to the flow layout
    MyFlowLayout *layout =
          (MyFlowLayout
*)self.collectionView.collectionViewLayout;
// If this is the start of the gesture
if (sender.state == UIGestureRecognizerStateBegan) {
    // Get the initial location of the pinch?
    CGPoint initialPinchPoint =
[sender locationInView:self.collectionView]; //Convert pinch location into a specific cell
NSIndexPath *pinchedCellPath =
             [self.collectionView
indexPathForItemAtPoint:initialPinchPoint];
        // Store the indexPath to cell
layout.currentCellPath = pinchedCellPath; }
else if (sender.state == UIGestureRecognizerStateChanged) {
// Store the new center location of the selected cell layout.currentCellCenter =
[sender locationInView:self.collectionView]; // Store the scale value
layout.currentCellScale = sender.scale; }
else {
[self.collectionView performBatchUpdates:^{ layout.currentCellPath = nil;
layout.currentCellScale = 1.0;
        } completion:nil];
} }

答案 3 :(得分:0)

出于性能原因,我建议您避免向每个单元格添加子视图和/或手势。相反,添加一个单一的longPressureGesture到您的collectionView和手势选择器处理您的每单元格切换业务逻辑。如下:

在viewDidLoad或设置集合的位置,添加以下手势:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showDeleteActions:)];
longPress.delegate = self;
[_aCollectionView addGestureRecognizer:longPress];

然后在选择器上进行单细胞处理:

- (void)showDeleteActions:(UILongPressGestureRecognizer*)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        NSIndexPath *indexPath = [_aCollectionView indexPathForItemAtPoint:[gesture locationInView:_aCollectionView]];
        UICollectionViewCell *cell = [_aCollectionView cellForItemAtIndexPath:indexPath];
        NSLog(@"cell to delete at IndexPath: %@", indexPath);
    }
}

这种方法效率更高,更稳定。

答案 4 :(得分:0)

这是导致您的UILongPressGestureRecognizer覆盖到最后一个单元格的原因。所以你需要为cellForRowAtIndexPath中的每个单元格创建UILongPressGestureRecognizer:

这是一个可以帮助你的小代码片段

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
    imageView.tag = 1;
    [imageView setBackgroundColor:[UIColor redColor]];
    UILongPressGestureRecognizer *longPressure =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePressue:)];
    longPressure.delegate = self;
    cell.userInteractionEnabled = YES; // Make userInteractionEnabled enable so it can detect touch
    [cell addGestureRecognizer:self.longPressure];
    [cell addSubview:imageView];
    return cell;

无需在.h文件中为UILongPressGestureRecognizer创建任何属性。并删除代码以在整个UICollectionView上添加userInteractionEnabled。