添加多个按钮+以编程方式在UICollectionViewCell SWIFT中接收touchUpInside事件
我试图在不使用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是否适合接受多种类型的点击操作(点击整个单元格而不是单元格内的多个按钮)?
这是我到目前为止所尝试的:
- 在CollectionView上禁用Tap Gesture,仍然没有接收事件
- 在没有向按钮添加tap事件的情况下,我试图在单元格内找到tap的“位置”,然后检查它是否在按钮的边界内,如果是,则触发事件。 当我尝试添加动画或滚动时,我发现这个工作有问题。
感谢您的建议和帮助。
您可以将手势识别器设置为不阻止子视图中的触摸
gestureRecognizer.cancelsTouchesInView = false
您还可以实现UIGestureRecognizerDelegate并将自己设置为委托。 然后你可以实现shouldReceiveTouch
optional public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
在那里,您可以检查目标视图,如果您不想对单元格手势识别器中的触摸作出反应,则返回false。
- iOS的uma arquiteturarobusta em seu projeto iOS
- 是否有可能更新configuration文件,而无需升级iOS企业应用程序?
- Swift中的cocoa豆和领域
- 添加文本字幕到video轨道(在Swift中)失败,错误代码-11841
- 给定一个CGPath,如何使其曲线?
- db executeUpdate …在FMDB块中并没有检查,没有错误
- AFHTTPRequestOperations队列创build内存堆积
- hover的CSSdynamic导航 – 如何让它在iOS Safari中工作?
- UITableViewCell上的UIPanGestureRecognizer覆盖UITableView的滚动视图手势识别器