簡體   English   中英

UITapGestureRecognizer在UICollectionView標頭上不起作用

[英]UITapGestureRecognizer doesn't work on UICollectionView header

我有一個帶有標題的UICollectionView

我想在用戶點擊標題時調用方法headerTapped

我試圖像這樣在``方法中的標題中添加一個UITapGestureRecognizer

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "likesHeader", forIndexPath: indexPath) as! LikesCollectionReusableView

    header.postsCounter.text = "\(self.likedBasicPosts.count)"

    //Adding gesture recognizer
    let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector(headerTapped()))
    tapRecognizer.numberOfTapsRequired = 1
    header.addGestureRecognizer(tapRecognizer)

     return header
}

發生了什么事:

當視圖加載headerTapped被調用時(甚至不點擊標題),然后當我點擊標題時,它甚至沒有被調用。

headerTapped()

private func likesHeaderWasTapped() {
        if self.expandedSections.containsObject(1) {
            self.expandedSections.removeObject(1)
        } else {
            self.expandedSections.addObject(1)
        }
        self.smallPhotosCollectionView.reloadSections(NSIndexSet(index: 1))
    }

這是為什么?

謝謝!

此示例中的headerTapped方法必須采用Apple UITapGestureRecognizer文檔中的形式

另請參閱UIGestureRecognzer文檔以獲取更多討論。

另外,在headerTapped方法中,您需要檢查手勢識別器state以便僅在輕headerTapped手勢結束時才觸發邏輯。

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == .Ended {
        // handling code
    }
}

暫無
暫無

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

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