简体   繁体   English

UIPanGestureRecognizer不会转发触摸

[英]UIPanGestureRecognizer does not forward touches

I have couple of UICollectionView in my UIViewController . 我的UIViewController有几个UICollectionView On top of the VCs view I have a UIGestureRecognizer . 在VC视图的顶部,我有一个UIGestureRecognizer

I would like the UIGestureRecognizer to fire up, make his logic and forward the touch to the underling views to their thing (like scrolling the collection views). 我希望UIGestureRecognizer启动,制作他的逻辑并将触摸转发到底层视图到他们的东西(比如滚动集合视图)。

I've set the property cancelsTouchesInView of the UIGestureRecognizer to NO . 我已将UIGestureRecognizer的属性cancelsTouchesInView设置为NO But it didn't help. 但它没有帮助。

These didn't help neither: 这些都没有帮助:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]]) {
        return YES;
    }
    return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]]) {
        return YES;
    }
    return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([gestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]]) {
        return YES;
    }
    return NO;
}

First of all, UIGestureRecognizer is an abstract base class. 首先, UIGestureRecognizer是一个抽象基类。 You have to specify one of its subclasses for usage: UITapGestureRecognizer , for example. 您必须指定其子类之一:例如UITapGestureRecognizer Next, you have to set its delegate and return YES from shouldRecognizeSimultaneouslyWithGestureRecognizer delegate method. 接下来,您必须设置其委托并从shouldRecognizeSimultaneouslyWithGestureRecognizer委托方法返回YES I think that it should work then. 我认为它应该工作。

But let me ask you something: Why don't you use your collection views delegate method didSelectItem:atIndexPath in the first place? 但是让我问你一些问题:为什么不首先使用你的集合视图委托方法didSelectItem:atIndexPath

I was stuck on a problem like yours about two years ago, but my problem was with a MKMapView and not a UICollectionView, but I believe the we can resolve it. 大约两年前我遇到了像你这样的问题,但我的问题是使用MKMapView而不是UICollectionView,但我相信我们可以解决它。

Did you check that maybe you are not receiving delegate messages because the UICollectionView is consuming the touch and your gesture is never reached? 您是否检查过您可能没有收到委托消息,因为UICollectionView正在消耗触摸并且您的手势永远不会到达?

  • Try attaching your gesture on the UICollectionVIew instead on your VCs's view. 尝试在UICollectionVIew上添加手势,而不是在VC的视图上。

It should work, but if not, try removing your UICollection just for test, attach the gesture on your VC's view and tap the view to have sure the gesture is working. 它应该可以工作,但如果没有,请尝试删除您的UICollection仅用于测试,将手势附加到VC的视图上并点击视图以确保手势正常工作。

  • If even without your UICollectionView on the view's hierarchy the UIGesture not works, you are not initialising it correctly. 如果即使在视图的层次结构中没有UICollectionView,UIGesture也不起作用,则表示您没有正确初始化它。

Ok, if your delegate is been called when your gesture is attached on the VC's view but not when you attach it on the UICollectionView, I'm pretty sure you are having some gesture conflicts between your gesture and the UICollectionView's ones. 好的,如果在VC的视图上附加了手势时调用了您的委托,而在UICollectionView上附加了手势时,我很确定您的手势和UICollectionView的手势之间存在一些手势冲突。

  • In this case, loop your UICollectionView's gestures and require a fail response from your gesture. 在这种情况下,循环您的UICollectionView的手势并要求您的手势失败响应。 Take this snippet: 拿这个片段:

     for (UIGestureRecognizer *gesture in collectionView.gestureRecognizers) { [gesture requireGestureRecognizerToFail:yourGesture]; } 

Or ... 要么 ...

Subclass a UIView, put it on the most top of your view controller, above your UICollectionView, and override 将UIView子类化,将其置于视图控制器的最顶层,在UICollectionView上方,并覆盖

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

method, do your stuff inside it, and return nil to pass the touch to the view above... 方法,在你的内容中做你的东西,并返回nil将触摸传递到上面的视图...

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    //Do your stuff here ... you can also
    //return self; if you want to consume the touch

    //You can make this UIView's subclasse a "TouchDetector layer"
    //and call a delegate from here to notify other classes that 
    //this view is been touched...

    //returning nil, the touch will not be blocked and will be reached
    //by the view above it.
    return nil; 
}

I hope one of the above resolves your problem. 我希望上面的一个能解决你的问题。 Or at least takes you to the right way... 或者至少带你走正路......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM