【问题标题】:UITapGestureRecognizer on UICollectionView header not working?UICollectionView 标头上的 UITapGestureRecognizer 不起作用?
【发布时间】:2016-12-21 23:07:16
【问题描述】:

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

 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")
}

【问题讨论】:

  • 表达式#selector(self.numberOfPostsViewTapped) 甚至可以编译吗?查看您的代码,正确的选择器应该是#selector(self.numberOfPostsViewTapped(sender:))
  • 我确实编译...我用你的行替换并得到错误ProfileViewController.swift:117:66: Value of type 'ProfileViewController' has no member 'numberOfPostsViewTapped(sender:)'

标签: ios swift uicollectionview uitapgesturerecognizer


【解决方案1】:

检查您的框架。你的 UICollectionView 在它的框架内吗?

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

【讨论】:

  • 我如何检查它是否在它的框架内?它显示正确link
  • 检查它的最简单方法可能是将帧记录在 UICollectionView 的 layoutSubviews 之类的地方,例如 NSLog(@"Frame: %@", NSStringFromCGRect(self.frame)); (Objective-C 语法,我不知道 Swift 语法,抱歉,我相信你知道我的意思)
  • 我添加了这条评论: override func viewDidLayoutSubviews() { print("LAYING OUT SUBVIEWS") print("Frame: %@", NSStringFromCGRect(collectionView.frame)); } 这就是打印的内容:LAYING OUT SUBVIEWS Frame: %@ {{0, 64}, {414, 607}} 我不确定这对我的问题意味着什么?
  • 好的,在这种情况下,我回答的可能不是您的问题。我知道,当视图的内容超出其父视图的框架时,即使视图的内容仍然显示,它们也无法与之交互,因为它们位于该父视图框架之外。这就是我认为可能是你的问题。
【解决方案2】:

我认为你需要将 userInteractionEnabled 设置为 true 到你的 headerView 以便点击会到达你的标签,它是你的 headerView 的子标签。

headerView.userInteractionEnabled = true

【讨论】:

  • 我添加了,但似乎没有任何效果。我什至不确定如何开始调试这个问题
【解决方案3】:

对您的代码进行了一些更改,这对我有用

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            guard
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "AddLinksReusableView", for: indexPath) as? AddLinksReusableView else {
                fatalError("Invalid view type")
            }
            let numberOfPostsViewSelector : Selector = #selector(self.imgVuTapped)
            let viewPostsViewGesture = UITapGestureRecognizer(target: self, action: numberOfPostsViewSelector)
            headerView.profileImg.isUserInteractionEnabled = true

            viewPostsViewGesture.numberOfTapsRequired = 1
            viewPostsViewGesture.delaysTouchesBegan = true
            headerView.profileImg.addGestureRecognizer(viewPostsViewGesture)
            return headerView
        default:
            assert(false, "Invalid element type")
        }
    }
    @objc func imgVuTapped (sender: UITapGestureRecognizer){
        print("HErE, Its working")
    }

快乐编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 2021-12-05
    • 1970-01-01
    • 2016-09-25
    • 2012-07-18
    • 2015-09-30
    • 2013-07-17
    • 2021-08-28
    相关资源
    最近更新 更多