添加多个按钮 + 以编程方式在 UICollectionViewCell SWIFT 中接收 touchUpInside 事件
Adding multiple buttons + receiving touchUpInside event inside UICollectionViewCell SWIFT programmatically
我正在尝试在 Swift 中以编程方式为列表构建 CollectionView,而不使用 Storyboard。我能够向单元格添加点击手势事件。
但是,当我将一组按钮添加到 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 上禁用点击手势,仍然没有收到事件
- 在没有向按钮添加点击事件的情况下,我试图在单元格内找到点击的 "location",然后检查它是否在按钮的范围内,如果是,则触发一个事件。当我尝试添加动画或滚动时,我发现此解决方法存在问题。
感谢您的建议和帮助。
您可以将手势识别器设置为不阻止子视图中的触摸
gestureRecognizer.cancelsTouchesInView = false
您还可以实现 UIGestureRecognizerDelegate 并将自己设置为委托。然后你可以实现 shouldReceiveTouch
optional public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
在那里你可以检查目标视图,如果你不想对单元格手势识别器中的触摸做出反应,return false。
我正在尝试在 Swift 中以编程方式为列表构建 CollectionView,而不使用 Storyboard。我能够向单元格添加点击手势事件。
但是,当我将一组按钮添加到 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 上禁用点击手势,仍然没有收到事件
- 在没有向按钮添加点击事件的情况下,我试图在单元格内找到点击的 "location",然后检查它是否在按钮的范围内,如果是,则触发一个事件。当我尝试添加动画或滚动时,我发现此解决方法存在问题。
感谢您的建议和帮助。
您可以将手势识别器设置为不阻止子视图中的触摸
gestureRecognizer.cancelsTouchesInView = false
您还可以实现 UIGestureRecognizerDelegate 并将自己设置为委托。然后你可以实现 shouldReceiveTouch
optional public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
在那里你可以检查目标视图,如果你不想对单元格手势识别器中的触摸做出反应,return false。