Ios 以编程方式在UICollectionViewCell SWIFT中添加多个按钮+接收touchUpInside事件
我正在尝试为一个列表构建一个CollectionView,所有这些都是在Swift中通过编程实现的,而不使用故事板。我可以在手机中添加一个点击手势事件 但是,当我向UICollectionViewCell的contentView添加一组按钮时,这些按钮不会接收任何触摸事件 内部控制器Ios 以编程方式在UICollectionViewCell SWIFT中添加多个按钮+接收touchUpInside事件,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我正在尝试为一个列表构建一个CollectionView,所有这些都是在Swift中通过编程实现的,而不使用故事板。我可以在手机中添加一个点击手势事件 但是,当我向UICollectionViewCell的contentView添加一组按钮时,这些按钮不会接收任何触摸事件 内部控制器 let sampleCollectionView = UICollectionView(frame: (..), collectionViewLayout: layout) //Register the UICo
let sampleCollectionView = UICollectionView(frame: (..), collectionViewLayout: layout)
//Register the UICollectionViewCell inside UICollectionView
sampleCollectionView.registerClass(sampleCollectionViewCell.self, forCellWithReuseIdentifier: "sampleCell")
//Add Tap Gesture event to the cell area
let tap = UITapGestureRecognizer(target: self, action: "handleTapForCell:")
sampleCollectionView.addGestureRecognizer(tap)
func handleTapForCell(recognizer: UITapGestureRecognizer){
//I can break in here
}
内部集合视图单元
class sampleCollectionViewCell: UICollectionViewCell
{
override init(frame: CGRect) {
var buttonBack = UIButton(type: .Custom)
buttonBack.addTarget(self, action: "actionGoBack:", forControlEvents: UIControlEvents.TouchUpInside)
..
self.contentView.addSubview(buttonBack)
}
func actionGoBack(sender: UIButton){
//I want to get my touch action break in here when I tap right inside the button but it won't
}
}
CollectionViewCell是否适合接受在整个单元格上点击的多种点击动作,而不是单元格内的多个按钮
以下是我迄今为止所做的尝试:
禁用CollectionView上的点击手势,仍不接收事件
在不向按钮添加点击事件的情况下,我尝试在单元格内找到点击的位置,然后检查这是否在按钮的范围内,如果是,则触发事件。当我尝试添加动画或滚动时,我发现这项工作很麻烦。
谢谢你的建议和帮助 您可以将手势识别器设置为不阻止子视图中的触摸
gestureRecognizer.cancelsTouchesInView = false
您还可以实现UIgestureRecognitzerDelegate并将自己设置为委派。然后您可以实现shouldReceiveTouch
optional public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
如果您不想对单元格手势识别器中的触摸做出反应,您可以检查哪个视图是目标视图,并返回false。为什么要在单元格中添加点击识别器?UICollectionView有didSelect的回调函数,对吗?是的,我在单元格级别交换了didSelect/tapGesure-对按钮没有影响。太棒了。只是调一下水龙头。cancelsTouchesInView=false,获取要识别的事件我必须单击两次。我现在要做的就是从resuable找到一个确切的单元来处理我的事件。谢谢