ios - UICollectionView header 上的 UITapGestureRecognizer 不起作用?

标签 ios swift uicollectionview uitapgesturerecognizer

我正在尝试将点击手势识别器添加到我的 UICollection View 的标题中,但无论如何,我都无法触发 numberOfPostsViewTapped() 函数。我已经尝试了几个小时,并尝试在标题 View 中使用其他 UI 元素(例如其他 View 或标签),但没有任何帮助。非常感谢一些指导。

 func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader: // only checking header - no footer on this view

        // use an external class for the header UICollectionViewCell in order to set outlets on a non-reusable cell
        // if you try to set outlets on a reusable cell, such as a header, it will fail
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! ProfileCollectionViewHeader

        // dynamically set user profile information
        headerView.usernameTextLabel.text = user?.name
        headerView.numberOfPostsTextLabel.text = user?.numberOfPosts != nil ? "\(user!.numberOfPosts!)" : "0"

        let numberOfPostsViewSelector : Selector = #selector(self.numberOfPostsViewTapped)
        let viewPostsViewGesture = UITapGestureRecognizer(target: self, action: numberOfPostsViewSelector)
        viewPostsViewGesture.numberOfTapsRequired = 1
        viewPostsViewGesture.delaysTouchesBegan = true
        headerView.numberOfPostsTextLabel.userInteractionEnabled = true;
        headerView.numberOfPostsTextLabel.addGestureRecognizer(viewPostsViewGesture)

        return headerView

    default:

        assert(false, "Unexpected element kind")

    }
}
func numberOfPostsViewTapped(sender: UITapGestureRecognizer){
    print("HErE")
}

最佳答案

检查你的框架。您的 UICollectionView 是否在其框架内?

如果一个 View 超出了它的框架,并且没有裁剪到它的边界,它会显示 View 的内容,但用户交互将无法使用它。

关于ios - UICollectionView header 上的 UITapGestureRecognizer 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41273437/

相关文章:

ios - 仅在获取 JSON 信息后使用数据

ios - 从函数返回 CGAffineTransform

ios - 将 Collection View 滚动到最后一个页脚 View

swift - 如何修复警告 `Invalid frame dimension (negative or non-finite).`

ios - 在占位符字段中创建空格

ios - 点击按钮时如何隐藏 collectionView header ?

Swift CollectionViewCells 问题

objective-c - block 不响应外部属性更改

iphone - IOS5无法使用核心数据库

swift - 在 switch 语句中使用 if let 语法