How can I detect a double tap on a certain cell in UITableView?

后端 未结 14 1052
悲哀的现实
悲哀的现实 2020-12-01 00:18

How can I detect a double tap on a certain cell in UITableView?

i.e. I want to perform one action if the user made a single touch and a

14条回答
  •  长情又很酷
    2020-12-01 00:57

    Here's my complete solution:

    CustomTableView.h

    //
    //  CustomTableView.h
    //
    
    #import 
    
    @interface CustomTableView : UITableView
    
        // Nothing needed here
    
    @end
    

    CustomTableView.m

    //
    //  CustomTableView.m
    //
    
    #import "CustomTableView.h"
    
    @implementation CustomTableView
    
    
    //
    // Touch event ended
    //
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
        // For each event received
        for (UITouch * touch in touches) {
    
            NSIndexPath * indexPath = [self indexPathForRowAtPoint: [touch locationInView:self] ];
    
            // One tap happened
            if([touch tapCount] == 1)
            {
                // Call the single tap method after a delay
                [self performSelector: @selector(singleTapReceived:)
                           withObject: indexPath
                           afterDelay: 0.3];
            }
    
    
            // Two taps happened
            else if ([touch tapCount] == 2)
            {
                // Cancel the delayed call to the single tap method
                [NSObject cancelPreviousPerformRequestsWithTarget: self
                                                         selector: @selector(singleTapReceived:)
                                                           object: indexPath ];
    
                // Call the double tap method instead
                [self performSelector: @selector(doubleTapReceived:)
                           withObject: indexPath ];
            }
    
    
        }
    
        // Pass the event to super
        [super touchesEnded: touches
                  withEvent: event];
    
    }
    
    
    //
    // Single Tap
    //
    -(void) singleTapReceived:(NSIndexPath *) indexPath
    {
        NSLog(@"singleTapReceived - row: %ld",(long)indexPath.row);
    }
    
    
    //
    // Double Tap
    //
    -(void) doubleTapReceived:(NSIndexPath *) indexPath
    {
        NSLog(@"doubleTapReceived - row: %ld",(long)indexPath.row);
    }
    
    
    
    @end
    

提交回复
热议问题