作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有标题的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
}
发生了什么:
当 View 加载 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 docs 本示例中的形式。
另请参阅UIGestureRecognzer docs进行更多讨论。
此外,在您的 headerTapped
方法中,您需要检查手势识别器state
,以便仅在点击手势结束时触发逻辑。
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
// handling code
}
}
关于ios - UITapGestureRecognizer 不适用于 UICollectionView header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38658735/
我是一名优秀的程序员,十分优秀!