簡體   English   中英

UIPanGestureRecognizer不會轉發觸摸

[英]UIPanGestureRecognizer does not forward touches

我的UIViewController有幾個UICollectionView 在VC視圖的頂部,我有一個UIGestureRecognizer

我希望UIGestureRecognizer啟動,制作他的邏輯並將觸摸轉發到底層視圖到他們的東西(比如滾動集合視圖)。

我已將UIGestureRecognizer的屬性cancelsTouchesInView設置為NO 但它沒有幫助。

這些都沒有幫助:

-(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;
}

首先, UIGestureRecognizer是一個抽象基類。 您必須指定其子類之一:例如UITapGestureRecognizer 接下來,您必須設置其委托並從shouldRecognizeSimultaneouslyWithGestureRecognizer委托方法返回YES 我認為它應該工作。

但是讓我問你一些問題:為什么不首先使用你的集合視圖委托方法didSelectItem:atIndexPath

大約兩年前我遇到了像你這樣的問題,但我的問題是使用MKMapView而不是UICollectionView,但我相信我們可以解決它。

您是否檢查過您可能沒有收到委托消息,因為UICollectionView正在消耗觸摸並且您的手勢永遠不會到達?

  • 嘗試在UICollectionVIew上添加手勢,而不是在VC的視圖上。

它應該可以工作,但如果沒有,請嘗試刪除您的UICollection僅用於測試,將手勢附加到VC的視圖上並點擊視圖以確保手勢正常工作。

  • 如果即使在視圖的層次結構中沒有UICollectionView,UIGesture也不起作用,則表示您沒有正確初始化它。

好的,如果在VC的視圖上附加了手勢時調用了您的委托,而在UICollectionView上附加了手勢時,我很確定您的手勢和UICollectionView的手勢之間存在一些手勢沖突。

  • 在這種情況下,循環您的UICollectionView的手勢並要求您的手勢失敗響應。 拿這個片段:

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

要么 ...

將UIView子類化,將其置於視圖控制器的最頂層,在UICollectionView上方,並覆蓋

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

方法,在你的內容中做你的東西,並返回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; 
}

我希望上面的一個能解決你的問題。 或者至少帶你走正路......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM