0

我已经在情节提要中初始化了一个 longPressGesture,我想将它添加到collectionView.

问题是:手势仅适用于最后添加的单元格collectionView

这是我的代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     MyCollectionCell *cell = (MyCollectionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    [cell addGestureRecognizer:longPressGesture];
    return cell;
}

.h 文件:

IBOutlet UILongPressGestureRecognizer *longPressGesture;
4

1 回答 1

1

如果您的手势目标方法对所有单元格执行相同的操作,则UILongPressGesture每次都创建新并将其分配给单元UICollectionView

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

     MyCollectionCell *cell = (MyCollectionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] 
                                          initWithTarget:self action:@selector(handleGesture:)];
     longPressGesture.minimumPressDuration = 1.5; 

    [showUserMap addGestureRecognizer:lpgr];

    [cell addGestureRecognizer:longPressGesture];
    return cell;
}
于 2013-03-13T13:20:39.350 回答