[英]Adding multiple buttons + receiving touchUpInside event inside UICollectionViewCell SWIFT programmatically
我试图在不使用Storyboard的情况下以编程方式在Swift中为列表构建CollectionView。 我能够向单元格添加一个轻击手势事件。
但是,当我向UICollectionViewCell的contentView添加一组按钮时,按钮不会收到任何触摸事件。
内部控制器
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
}
在CollectionViewCell里面
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是否适合接受多种类型的点击操作(点击整个单元格而不是单元格内的多个按钮)?
这是我到目前为止尝试的内容:
感谢您的建议和帮助。
您可以将手势识别器设置为不阻止子视图中的触摸
gestureRecognizer.cancelsTouchesInView = false
您还可以实现UIGestureRecognizerDelegate并将自己设置为委托。 然后你可以实现shouldReceiveTouch
optional public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
在那里,您可以检查哪个视图是目标,如果您不想对单元格手势识别器中的触摸作出反应,则返回false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.