如何仅检测不在整个集合视图上的单元格?

时间:2015-11-25 09:57:43

标签: ios objective-c iphone ipad

我的应用中有collectionView。我有一个要求,我应该能够加倍和加倍单击单元格以执行不同的操作。使双重和双重成为可能collectionView上的单击手势我在收集视图中添加了手势&通过以下代码获得位置。

-(void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer
{
  if([arr_userAlbums count]>0)
  {
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
    {
      return;
    }
    p = [gestureRecognizer locationInView:self.collection_view];

    NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];

    celltTapped_index_path=indexPath;
  }
}

-(void)handleDoubleTap:(UITapGestureRecognizer *)gestureRecognizer
{
  if([arr_userAlbums count]>0)
  {
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
    {
      return;
    }
    p = [gestureRecognizer locationInView:self.collection_view];

    NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];

    celltTapped_index_path=indexPath;
  }
}

但是在这种情况下,整个屏幕甚至在细胞不可见的地方,它接受双击和放大器。单击。我想检测单身&双击仅在单元格上而不是整个集合视图。

2 个答案:

答案 0 :(得分:1)

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

 static NSString *cellIdentifier = @"cvCell";
 customCell *cell = (customCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

 cell.img_Collection.image = [imgArray objectAtIndex:indexPath.row];
 cell.lbl_Collection.text = [lblArray objectAtIndex:indexPath.row];

 cell.tag = indexPath.row;

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
 singleTap.numberOfTapsRequired = 1;
 singleTap.delaysTouchesEnded = YES;

 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
 doubleTap.numberOfTapsRequired = 2;


 [singleTap requireGestureRecognizerToFail:doubleTap];

 [cell addGestureRecognizer:singleTap];
 [cell addGestureRecognizer:doubleTap];

 return cell;
}



-(void)handleSingleTap:(UIGestureRecognizer *)recognizer
{
   NSLog(@"The single tap happened for %ld th index",recognizer.view.tag);
}


-(void)handleDoubleTap:(UIGestureRecognizer *)recognizer
{
  NSLog(@"The Double tap happened for %ld th index",recognizer.view.tag);
}

答案 1 :(得分:-1)

你可以使用像这样的hitTest方法来点击View

UIView *tappedView = [self hitTest:yourLocation forEvents:nil];

这将返回您点击的视图  如果条件如此

,请检查此项
if(tappedView == Imageview)
{
    // do this
}
else if(tappedView == CollectionViewCell)
{
    // do this
}