简体   繁体   English

在所有UICollectionViewCells上应用手势

[英]applying gesture on all UICollectionViewCells

in my app i have a UICollectionView. 在我的应用程序中,我有一个UICollectionView。 i want to apply to it's cells a long pressure gesture. 我想对它的单元格施加长压力手势。 i implemented that but when i run the app only the last cell works and the others do not respond. 我实现了这一点,但是当我运行该应用程序时,只有最后一个单元格起作用,其他单元格没有响应。 what it wrong? 怎么了 here is my code. 这是我的代码。

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

in view did load: 鉴于确实加载:

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

self.longPressure.delegate = self;

[self.collectionView addGestureRecognizer:self.longPressure];

gesture handler: 手势处理程序:

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

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

in collectionView:cellforitemAtIndexPAth: 在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;

is there anything wrong? 有什么问题吗?

you need to add gesture on the each cell not for the UICollection View. 您需要在每个单元格上添加手势,而不是为UICollection视图添加手势。 that might be your problem.the last cell works because the gesture is added to the last cell only. 可能是您的问题。最后一个单元格起作用,因为手势仅添加到了最后一个单元格。

You need to creat long press gesture for each cell. 您需要为每个单元格创建长按手势。 Because gesture like other ui control,only one in the project.If you want add it to other view,you need copy another one. 因为手势与其他ui控件一样,所以项目中只有一个。如果要将其添加到其他视图中,则需要复制另一个。

Have a look at following Code snippet 看看下面的代码片段

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

Handling the Pinch: 处理捏:

- (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];
} }

For performance reasons I suggest you to avoid adding subviews and/or gestures to every single cell. 出于性能原因,我建议您避免向每个单元格添加子视图和/或手势。 Instead, add a singe longPressureGesture to your collectionView and on the gesture selector handle your per-cell switch business logic. 取而代之的是,向您的collectionView添加一个longlongPressureGesture并在手势选择器上处理您的每单元开关业务逻辑。 Something as follow: 如下:

On your viewDidLoad or where you setup your collection, add the following gesture: 在viewDidLoad或设置集合的位置,添加以下手势:

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

then on the selector do the single-cell handling: 然后在选择器上执行单细胞处理:

- (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);
    }
}

this approach is much more efficient abd far more stable. 这种方法效率更高,稳定性更高。

Here what it is causing that your UILongPressGestureRecognizer getting overwrite to last cell. 这是导致您的UILongPressGestureRecognizer覆盖到最后一个单元格的原因。 So you need to create UILongPressGestureRecognizer for each cell in cellForRowAtIndexPath: 因此,您需要为cellForRowAtIndexPath中的每个单元格创建UILongPressGestureRecognizer:

Here is a small code snippet that can help you out 这是一个可以帮助您的小代码段

    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;

There is no need to create any property for the UILongPressGestureRecognizer in .h file. 无需在.h文件中为UILongPressGestureRecognizer创建任何属性。 And also remove code to add userInteractionEnabled on whole UICollectionView. 并删除代码以在整个UICollectionView上添加userInteractionEnabled。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM