[英]Adding multiple buttons + receiving touchUpInside event inside UICollectionViewCell SWIFT programmatically
I am trying to build a CollectionView for a list all programmatically in Swift without using Storyboard. 我试图在不使用Storyboard的情况下以编程方式在Swift中为列表构建CollectionView。 I was able to add a tap gesture event to the cell. 我能够向单元格添加一个轻击手势事件。
However, When I added a set of buttons to the UICollectionViewCell's contentView, the buttons do not receive any touch events. 但是,当我向UICollectionViewCell的contentView添加一组按钮时,按钮不会收到任何触摸事件。
Inside Controller 内部控制器
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
}
Inside CollectionViewCell 在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
}
}
Is CollectionViewCell suited to accept more than one type of tap action (tapping on the whole cell versus multiple buttons inside cell)? CollectionViewCell是否适合接受多种类型的点击操作(点击整个单元格而不是单元格内的多个按钮)?
Here is what I tried so far: 这是我到目前为止尝试的内容:
Thanks for your suggestions and help. 感谢您的建议和帮助。
You can set the gesture recognizer to not block the touches in subview 您可以将手势识别器设置为不阻止子视图中的触摸
gestureRecognizer.cancelsTouchesInView = false
You can also implement UIGestureRecognizerDelegate and set yourself as delegate. 您还可以实现UIGestureRecognizerDelegate并将自己设置为委托。 Then you can implement shouldReceiveTouch 然后你可以实现shouldReceiveTouch
optional public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
There you can check which view is targeted and return false if you don't want to react to the touch in the cells gesture recognizer. 在那里,您可以检查哪个视图是目标,如果您不想对单元格手势识别器中的触摸作出反应,则返回false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.