我正在尝试将点击手势识别器添加到我的 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/