簡體   English   中英

為什么UICollectionView在滾動其單元格的scrollView后不再調用didSelectItemAtIndexPath?

[英]why UICollectionView not calling didSelectItemAtIndexPath any more after scrolling the scrollView of it's cells?

我正在使用UICollectionView來顯示一些產品。 在自定義UICollectionViewCell ,有一個自定義UIScrollView ,其中包含一些允許用戶進行快速預覽的圖像。

要將點擊手勢從UIScrollView轉發到UICollectionViewCell ,我重寫了與觸摸相關的方法,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging) {
        NSLog(@"Began ==>");
        [self.nextResponder touchesBegan:touches withEvent:event];
    }
    else {
        [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging) {
        NSLog(@"Moved ==>");
        [self.nextResponder touchesMoved:touches withEvent:event];
    }
    else {
        [super touchesMoved:touches withEvent:event];
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging) {
        NSLog(@"Ended ==>");
        [self.nextResponder touchesEnded:touches withEvent:event];
    }
    else {
        [super touchesEnded:touches withEvent:event];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging) {
        NSLog(@"Cancelled ==>");
        [self.nextResponder touchesCancelled:touches withEvent:event];
    }
    else {
        [super touchesCancelled:touches withEvent:event];
    }
}

有時在某些情況下不起作用。 例如,當第一次加載UICollectionView時,可以在單擊時調用didSelectItemAtIndexPath方法,但是在進行一些滾動或點擊后,即使您嘗試重新加載UICollectionView ,該方法也不再被調用,它仍然無法正常工作。

我嘗試將消息記錄在UICollectionViewCell的touch方法中,將touches和event轉發。 但是,如果單元格獲得手勢, UICollectionView要調用UICollectionViewdidSelectItemAtIndexPath

任何建議將不勝感激。

更新: UICollectionViewCell是從nib文件加載的。

如果您要針對特定​​對象,那么使用nextResponder並不是一個好主意。 就像您親眼所見,響應者鏈可能會更改,並且您不一定總能獲得所需的對象。 發生這種情況的原因可能有多種,但無論如何,在這種情況下,您都應該使用更可靠的方法。

由於UIScrollViewUICollectionViewCell的子視圖,因此您可以將事件直接傳遞給您的[self.superview touchesMoved:touched withEvent:event];視圖[self.superview touchesMoved:touched withEvent:event]; 執行此操作時,您需要牢記視圖層次結構,並且可能必須調用超級視圖的超級視圖才能到達單元格。

我也不建議使用觸摸來攔截用戶交互。 我強烈建議使用UIGestureRecognizers,因為它將提供更好,更一致的功能。

您應該始終調用super方法。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSLog(@"Began ==>");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    NSLog(@"Moved ==>");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    NSLog(@"Ended ==>");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    NSLog(@"Cancelled ==>");
}

其實這是因為self.dragging理解力不足。 向前輕擊手勢的正確方法如下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging) {
        [self.nextResponder touchesEnded:touches withEvent:event];
    }
    else {
        [super touchesEnded:touches withEvent:event];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesCancelled:touches withEvent:event];
}

暫無
暫無

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

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