Gesture Recognizers and TableView

前端 未结 3 379
日久生厌
日久生厌 2021-02-02 17:13

I have a UIView which covers all of a UITableView. The UIView is using gesture recognizers for control of what the table displays. I still need the vertical UITableView scrollin

相关标签:
3条回答
  • 2021-02-02 17:53

    I tried Rob Bonner's suggestion and it works great. thank you.

    But, in my case, there's a problem with direction recognition. (recognizer.direction always refer 3) I'm using IOS5 SDK and Xcode 4.

    It seems caused by "[gesture setDirection:(left | right)]" I think. (because predefined (dir left | dir right) calculation result is 3)

    So, if someone has problem like me and want to recognize swipe left and right seprately, then assign two recognizer to table view with different directions.

    Like this:

    UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] 
                                                 initWithTarget:self
                                                 action:@selector(handleSwipeLeft:)];
    [swipeLeftGesture setDirection: UISwipeGestureRecognizerDirectionLeft];
    
    UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] 
                                                  initWithTarget:self 
                                                  action:@selector(handleSwipeRight:)];
    
    [swipeRightGesture setDirection: UISwipeGestureRecognizerDirectionRight];
    
    [tableView addGestureRecognizer:swipeLeftGesture];
    [tableView addGestureRecognizer:swipeRightGesture];
    

    and gesture action below:

    - (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
        [self moveLeftColumnButtonPressed:nil];
    }
    
    - (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
        [self moveRightColumnButtonPressed:nil];
    }
    

    I coded with ARC feature then if you are not using ARC, add release codes.

    PS: My english is not so good, so if there's any sentential error, correction will be very pleased :)

    0 讨论(0)
  • 2021-02-02 17:56

    If you need to know your cell's indexPath:

    - (void)handleSwipeFrom:(UIGestureRecognizer *)recognizer {
        CGPoint swipeLocation = [recognizer locationInView:self.tableView];
        NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
        UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
    }
    

    This was previously answered in UIGestureRecognizer and UITableViewCell issue.

    0 讨论(0)
  • 2021-02-02 17:57

    Assign your gesture to the table view and the table will take care of it:

    UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc]
            initWithTarget:self action:@selector(handleSwipeFrom:)];
    [gesture setDirection:
            (UISwipeGestureRecognizerDirectionLeft
            |UISwipeGestureRecognizerDirectionRight)];
    [tableView addGestureRecognizer:gesture];
    [gesture release];
    

    Then in your gesture action method, act based on the direction:

    - (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
        if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
            [self moveLeftColumnButtonPressed:nil];
        }
        else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
            [self moveRightColumnButtonPressed:nil];
        }
    }
    

    The table will only pass you the gestures you have asked for after handling them internally.

    0 讨论(0)
提交回复
热议问题