UIPanGestureRecognizer与scrollview冲突

时间:2013-02-01 16:42:02

标签: ios uiscrollview uigesturerecognizer uipangesturerecognizer

我正在尝试将平移手势识别器添加到包含滚动视图的视图中,但我想我的优先级存在问题。

我的全局UIView有一个像这样设置的UIPanGestureRecognizer:

_bottomPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(bottomPanGestureDetected:)];
_bottomPanGestureRecognizer.minimumNumberOfTouches = 2;
_bottomPanGestureRecognizer.maximumNumberOfTouches = 2;
_bottomPanGestureRecognizer.delaysTouchesBegan = NO;
_bottomPanGestureRecognizer.delaysTouchesEnded = NO;

我想要识别这个手势,从底部显示另一个视图,并进行一些向下的捏合。

问题是scrollview在我之前识别它自己的平移手势。

所以我试图延迟它,这要归功于:

[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:_bottomPanGestureRecognizer];

它正在工作,在我的两个手指向上到识别器后触发了scrollview事件,但问题是现在当我只用一根手指在滚动视图中滚动时,滚动工作一小段延迟。

我想对此次活动没有任何延误,这可能吗?欢迎任何想法!

干杯。

西里尔

3 个答案:

答案 0 :(得分:7)

如果还没有解决,我为我解决了问题。

我在UIScrollView中添加了一个UIPanGestureRecognizer来检测两个手指平移手势,默认的UIScrollView行为(滚动到某些东西)仍然有效。

所以我做的是将UIPanGestureReconizer添加到UIScrollView:

UIPanGestureRecognizer *pangestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(displayReloadIndicator:)];
pangestureRecognizer.minimumNumberOfTouches = 2;
pangestureRecognizer.delegate = self;
[self.scrollView addGestureRecognizer:pangestureRecognizer];
[pangestureRecognizer release];

在此之后我添加了代码:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}

在此之后,我实现了平移手势识别器动作方法。

- (void) displayReloadIndicator:(UIPanGestureRecognizer*) panGestureRecognizer {

    UIGestureRecognizerState gestureRecognizerState = gestureRecognizer.state;

    CGPoint translation = [gestureRecognizer translationInView:self.scv_bibgesamt];

    if (gestureRecognizerState == UIGestureRecognizerStateBegan) {

        // create a UIView with all the Pull Refresh Headers and add to UIScrollView
        // This is really much lines of code, but its simply creating a UIView (later you'll find a myRefreshHeaderView, which is my base view) and add UIElements e.g. UIActivityIndicatorView, a UILabel and a UIImageView on it
        // In iOS 6 you will also have the possibility to add a UIRefreshControl to your UIScrollView

    }

    else if (gestureRecognizerState == UIGestureRecognizerStateEnded
             || gestureRecognizerState == UIGestureRecognizerStateCancelled) {

        if (translation.y >= _myRefreshHeaderView.frame.size.height + 12) { // _myRefreshHeaderView is my baseview

         //so the UIScrollView has been dragged down with two fingers over a specific point and have been release now, so we can refresh the content on the UIScrollView
         [self refreshContent];

         //animatly display the refresh view as the top content of the UIScrollView
         [self.scrollView setContentOffset:CGPointMake(0, myRefreshHeaderView.frame.size.height) animated:YES];
    }

    else {

         //the UIScrollView has not been dragged over a specific point so don't do anything (just scroll back to origin)
         [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

         //remove the view (because it's no longer needed)
         [_myRefreshHeaderView removeFromSuperview];
    }
}

<强>更新

如果您希望集成导航控制器中的滑动功能,则应集成以下代码:

- (void) viewDidLoad {

    [super viewDidLoad];


    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

        self.navigationController.interactivePopGestureRecognizer.enabled = YES;

        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }

    //setup view controller
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if (gestureRecognizer == _panGestureRecognizer
        && [self.navigationController.viewControllers count] > 1) {

        CGPoint point = [touch locationInView:self.view.window];

        if (point.x < 20
            || point.x > self.view.window.frame.size.width - 20) {

            return NO;
        }
    }

    return YES;
} 

答案 1 :(得分:2)

实现panRecognizer委托以启用同时识别UIScrollView UIGestureRecognizer

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (_panRecognizer == gestureRecognizer) {
        if ([otherGestureRecognizer.view isKindOfClass:UIScrollView.class]) {
            UIScrollView *scrollView = (UIScrollView *)otherGestureRecognizer.view;
            if (scrollView.contentOffset.x == 0) {
                return YES;
            }
        }
    }

    return NO;
}

答案 2 :(得分:0)

SWIFT 4

如果使用的是scrollView,则可以在下面使用,当scrollView拖动到最顶部时,它只是一个panGestureRecognizer:

translation.y> 0表示您从上到下移动 locationInScrollView.y <500.0表示您将拖动结束于500或更小(可以自定义),以防止刷新在滚动条的中间或底部进行。

use strict;
use warnings;
use List::UtilsBy 'nsort_by';
my %hash = (
  name => {
     pos => 1
  },
  name_xxx => {
     pos => 2
  },
  name_yyy => {
     pos => 3
  },
);
my @sorted_keys = nsort_by { $hash{$_}{pos} } keys %hash;