[英]UITapGestureRecognizer doesn't work on UICollectionView header
I have a UICollectionView
with an header. 我有一个带有标题的
UICollectionView
。
I want to call the method headerTapped
when the user tap on the header. 我想在用户点击标题时调用方法
headerTapped
。
I've tried to add a UITapGestureRecognizer
to the header in the `` method like that: 我试图像这样在``方法中的标题中添加一个
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
}
And what happened it that: 发生了什么事:
When the view load headerTapped
is being called (without even tapping on the header), and then when I tap on the header it does;t even being called. 当视图加载
headerTapped
被调用时(甚至不点击标题),然后当我点击标题时,它甚至没有被调用。
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))
}
Why is that? 这是为什么?
Thank you! 谢谢!
Your headerTapped
method needs to be of the form in this example from the Apple UITapGestureRecognizer docs 此示例中的
headerTapped
方法必须采用Apple UITapGestureRecognizer文档中的形式
Also see the UIGestureRecognzer docs for more discussion. 另请参阅UIGestureRecognzer文档以获取更多讨论。
Also, in your headerTapped
method, you need to check the gesture recognizer state
so that you only trigger your logic when the tap gesture has ended. 另外,在
headerTapped
方法中,您需要检查手势识别器state
以便仅在轻headerTapped
手势结束时才触发逻辑。
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
// handling code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.