摘要
UICollectionViewCell子类能否阻止将didSelectItemAt: indexPath发送到其某些子视图()上的UICollectionViewDelegate for taps ,但对其他子视图则是正常的?
用例
我有一个代表文章摘要的UICollectionViewCell。对于大多数文章,当它们被点击时,我们浏览以显示文章。
但是,一些文章摘要显示了内联视频预览。当视频预览被点击时,我们不应该浏览,但是当文章摘要的其他区域被点击(标题)时,我们应该浏览。
我希望文章摘要单元格能够决定是否应该将点击作为选择。
发布于 2018-01-15 09:55:07
tapGestureRecogniser。tapGestureRecogniser选择器方法,并且手势将不会传递给委托。发布于 2018-01-15 10:05:11
您需要做的是将UITapGestureRecognizer附加到视图中,并从视图中监视水龙头:
class MyViewController: UIViewController, UICollectionViewDataSource, MyCellDelegate {
var dataSource: [Article] = []
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ArticleCell", for: indexPath) as! MyCell
cell.article = dataSource[indexPath.row]
cell.delegate = self
return cell
}
func articleDidTap(_ article: Article) {
// do what you need
}
}
// your data model
struct Article {}
protocol MyCellDelegate: class {
func articleDidTap(_ article: Article)
}
class MyCell: UICollectionViewCell {
var article: Article! {
didSet {
// update your views here
}
}
weak var delegate: MyCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(MyCell.tap)))
}
@objc func tap() {
delegate?.articleDidTap(article)
}
}这应该可以工作,因为您的视频视图应该重叠根视图,并防止从手势识别器接收点击。
https://stackoverflow.com/questions/48260318
复制相似问题