简体   繁体   English

添加多个按钮+以编程方式在UICollectionViewCell SWIFT中接收touchUpInside事件

[英]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: 这是我到目前为止尝试的内容:

  1. Disable Tap Gesture on CollectionView, still not receiving events 在CollectionView上禁用Tap Gesture,仍然没有接收事件
  2. Without adding tap events to button, I tried to find the "location" of tap inside the cell, then check if this is in bounds of the button, if so, fire an event. 在没有向按钮添加tap事件的情况下,我试图在单元格内找到tap的“位置”,然后检查它是否在按钮的边界内,如果是,则触发事件。 I find this work around to be buggy when I try to add animation or scroll. 当我尝试添加动画或滚动时,我发现这个工作有问题。

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.

相关问题 在swift中以编程方式向工具栏添加按钮 - Adding buttons to toolbar programmatically in swift 快速,以编程方式更改UICollectionViewCell和UILabel(在单元格内)的宽度 - Swift, Change width of UICollectionViewCell and UILabel(inside the cell) programmatically UICollectionViewCell中没有出现多个按钮 - Multiple Buttons not appearing in UICollectionViewCell Swift - 以编程方式更改 UICollectionViewCell 大小 - Swift - Change UICollectionViewCell size programmatically 将touchupinside添加到以编程方式创建的按钮,并将NSDate提交到文本字段 - Adding touchupinside to a programmatically created button, and submitting a NSDate to a textfield 以编程方式在特定 UICollectionViewCell 内的 TableView? - TableView inside specific UICollectionViewCell programmatically? 以编程方式创建的按钮不会收到事件内部的修饰…UICollectionViewCell - Buttons created programmatically doesn't receive touch up inside events… UICollectionViewCell 按钮单击事件(UIButton TouchUpInside操作)不起作用-iOS / Swift - Button click event (UIButton TouchUpInside action) not working - IOS/Swift Swift UICollectionViewController 与 UICollectionViewCell 内的参数 - Swift UICollectionViewController with parameter inside UICollectionViewCell UIScrollView按钮 - TouchUpInside? - UIScrollView of buttons - TouchUpInside?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM