How to detect double-taps on cells in a UICollectionView

前端 未结 5 835
野的像风
野的像风 2021-01-30 11:01

I want to respond to double-taps on cells in a UICollectionView, and have a double-tap action cancel cell selection.

This is what I\'ve tried:

UITapGestu         


        
5条回答
  •  心在旅途
    2021-01-30 11:36

    My solution was to not implement collectionView:didSelectItemAtIndexPath but to implement two gesture recognizers.

        self.doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processDoubleTap:)];
        [_doubleTapGesture setNumberOfTapsRequired:2];
        [_doubleTapGesture setNumberOfTouchesRequired:1];   
    
        [self.view addGestureRecognizer:_doubleTapGesture];
    
        self.singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processSingleTap:)];
        [_singleTapGesture setNumberOfTapsRequired:1];
        [_singleTapGesture setNumberOfTouchesRequired:1];
        [_singleTapGesture requireGestureRecognizerToFail:_doubleTapGesture];
    
        [self.view addGestureRecognizer:_singleTapGesture];
    

    This way I can handle single and double taps. The only gotcha I can see is that the cell is selected on doubleTaps but if this bothers you can you handle it in your two selectors.

提交回复
热议问题