5

私はUIScrollViewを含むカスタムUICollectionViewCellサブクラスを持っています。UICollectionViewCellのサブビューとしてのUIScrollView - スーパービューにタップを渡します

スクロールビューは正しくスクロールしますが、タップを傍受するため、コレクションビューのセルの強調表示と選択が期待通りに機能しません。

userInteractionEnabledからNOに設定すると「タップスルー」が許可されますが、スクロールは機能しません(もちろん)。

オーバーライドhitTest:withEvent:は、転送前にタップかパンかどうかを知る必要がないため、使用しません。

どのような考えですか?

答えて

1

今日はこの問題に遭遇しました。ここで私はそれをどのように解決したのですが、よりよい方法が必要です。コレクションビューの選択ロジックをセルコードに入れる必要はありません。

スクロールビューにUITapGestureRecognizerを追加します。

-(void) scrollViewTapped:(UITapGestureRecognizer *)sender { 
    UIView *tappedView = [sender view]; 

    while (![tappedView isKindOfClass:[UICollectionView class]]) { 
     tappedView = [tappedView superview]; 
    } 

    if (tappedView) { 
     UICollectionView *collection = (UICollectionView *)tappedView; 
     NSIndexPath *ourIndex = [collection indexPathForCell:self]; 
     BOOL isSelected = [[collection indexPathsForSelectedItems] containsObject:ourIndex]; 

     if (!isSelected) { 
      BOOL shouldSelect = YES; 
      if ([collection.delegate respondsToSelector:@selector(collectionView:shouldSelectItemAtIndexPath:)]) { 
       shouldSelect = [collection.delegate collectionView:collection shouldSelectItemAtIndexPath:ourIndex]; 
      } 

      if (shouldSelect) { 
       [collection selectItemAtIndexPath:ourIndex animated:NO scrollPosition:UICollectionViewScrollPositionNone]; 
       if ([collection.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) { 
        [collection.delegate collectionView:collection didSelectItemAtIndexPath:ourIndex]; 
       } 
      } 
     } else { 
      BOOL shouldDeselect = YES; 
      if ([collection.delegate respondsToSelector:@selector(collectionView:shouldDeselectItemAtIndexPath:)]) { 
       shouldDeselect = [collection.delegate collectionView:collection shouldDeselectItemAtIndexPath:ourIndex]; 
      } 

      if (shouldDeselect) { 
       [collection deselectItemAtIndexPath:ourIndex animated:NO]; 
       if ([collection.delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)]) { 
        [collection.delegate collectionView:collection didDeselectItemAtIndexPath:ourIndex]; 
       } 
      } 
     } 
    } 
} 

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)]; 
[scrollView addGestureRecognizer:tapGesture]; 

その後、コールバックでは、セルの上に、通常のタップに何が起こるかをシミュレートする必要があり

関連する問題