Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UICollectionViewController中触摸开始不触发_Ios_Swift_Uicollectionview_Touchesbegan_Uicollectionviewdelegate - Fatal编程技术网

Ios 在UICollectionViewController中触摸开始不触发

Ios 在UICollectionViewController中触摸开始不触发,ios,swift,uicollectionview,touchesbegan,uicollectionviewdelegate,Ios,Swift,Uicollectionview,Touchesbegan,Uicollectionviewdelegate,我有一个UICollectionViewController类,它布置了一堆单元格。当用户触摸一个单元格时,我想使用touchsbegind方法防止新的触摸,直到第一次触摸完成对单元格的选择 我试着把下面的代码放到我的视图控制器中,但它从未被调用 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesBegan(touches, withEvent

我有一个
UICollectionViewController
类,它布置了一堆单元格。当用户触摸一个单元格时,我想使用
touchsbegind
方法防止新的触摸,直到第一次触摸完成对单元格的选择

我试着把下面的代码放到我的视图控制器中,但它从未被调用

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    super.touchesBegan(touches, withEvent: event)
    print("touches began")
}
为什么这段代码在一个类中工作而在另一个类中不工作?我如何在我的
UICollectionViewController
中使用
touchsbegind
来检测我的收藏视图单元格上的触摸?请使用Swift解决方案。谢谢你的时间

研究: 我签出了此解决方案: 但答案大多适用于该特定项目

我尝试使用手势识别器运行
self.collectionView?.userInteractionEnabled=false
,但手势识别器不为
selector.state=.start
,仅为
.end
。所以我不能用它来阻止进一步的接触

我还尝试使用
UILongPressGestureRecognitizer
执行相同的操作,但该手势阻止了集合视图单元格侦听的默认点击手势,因此从未收到对单元格的常规点击,因此无法选择单元格


我发布了一个解决方案作为答案,虽然它没有回答原始问题。

这是一种解决方案,但不是使用
触摸开始
我使用了协议,它可以让我在
UICollectionView
中选择一个单元格

// The specified item should be selected.
override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    return true
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    colorIndex = (indexPath as NSIndexPath).item // sets color index to the selected cell's path
    let selectedCell = collectionView.cellForItem(at: indexPath) as UICollectionViewCell!
print("Cell selected: \(selectedCell)")

我怀疑你的一个手机可能没有将触碰发送到应答器链。如果您有自定义的集合视图单元格,请确保它们正在调用
super.touchesbeated…
我不知道为什么它没有调用(我认为圣诞老人在正确的路径上),但是您是否可以使用
var hasbeentoched:Bool
变量,该变量在第一次运行代码后设置为
true
?然后,如果hasbeentoched==false,那么要运行的代码将全部放在
块中。@Tim-我确实尝试过这个想法。在
collectionView(:didSelectItemAtIndexPath)
中,如果
hasBeenTouched==false
我将其设置为
true
,禁用用户交互,并运行一个块来处理单元格选择。在块的末尾,我将
hasbeintouch
设置为返回false并启用用户交互。但由于某些原因,仍有可能同时选择两个单元格,从而导致单元格选择功能“锁定”。我真的不明白为什么这种方法不起作用。@SantaClaus-我有自定义的集合视图单元格。要为单元格调用
super.touchsbegind
,我是否在我的
UICollectionViewController
子类的
collectionView:cellForItemAtIndexPath:
方法中进行调用?您是否阅读了文档[此处?][1][1]::您必须覆盖所有方法或不覆盖任何方法。也许这就是你们两个控制器的区别。
// The specified item should be selected.
override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    return true
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    colorIndex = (indexPath as NSIndexPath).item // sets color index to the selected cell's path
    let selectedCell = collectionView.cellForItem(at: indexPath) as UICollectionViewCell!
print("Cell selected: \(selectedCell)")