Detect horizontal panning in UITableView

前端 未结 5 2097
日久生厌
日久生厌 2021-01-31 16:42

I\'m using a UIPanGestureRecognizer to recognize horizontal sliding in a UITableView (on a cell to be precise, though it is added to the table itself). However, this gesture rec

5条回答
  •  盖世英雄少女心
    2021-01-31 17:13

    My answer is the same as Florian Mielke's, but I've simplified and corrected it some.

    How to use:

    Simply give your UIPanGestureRecognizer a delegate (UIGestureRecognizerDelegate). For example:

    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    panner.delegate = self;
    [self addGestureRecognizer:panner];
    

    Then have that delegate implement the following method:

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
        CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:gestureRecognizer.view.superview];
        return fabsf(translation.x) > fabsf(translation.y);
    }
    

提交回复
热议问题