Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Swift:切换视图后使用UILongPressGestureRecognitor时应用程序崩溃_Swift_View_Crash_Switch Statement_Gesture - Fatal编程技术网

Swift:切换视图后使用UILongPressGestureRecognitor时应用程序崩溃

Swift:切换视图后使用UILongPressGestureRecognitor时应用程序崩溃,swift,view,crash,switch-statement,gesture,Swift,View,Crash,Switch Statement,Gesture,在我看来,我包括了两种不同的UIGestureRecognitor,UILongPressGestureRecognitor和UITapGestureRecognitor。一个用于重新排列我的UICollectionView图像,另一个用于简单点击,从而获得所选图像的详细视图 一开始一切都正常,但当我展示完详细的图像视图后返回视图时,每当我尝试拖动图像时,应用程序就会崩溃。轻敲功能每次都会工作,但在开始拖动图像后,会立即发生崩溃 override func viewDidLoad() {

在我看来,我包括了两种不同的UIGestureRecognitor,UILongPressGestureRecognitor和UITapGestureRecognitor。一个用于重新排列我的UICollectionView图像,另一个用于简单点击,从而获得所选图像的详细视图

一开始一切都正常,但当我展示完详细的图像视图后返回视图时,每当我尝试拖动图像时,应用程序就会崩溃。轻敲功能每次都会工作,但在开始拖动图像后,会立即发生崩溃

override func viewDidLoad() {
    super.viewDidLoad()

    self.longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
    self.longPressGesture.delegate = self

    self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTapGesture(gesture:)))
    self.tapGesture.delegate = self

    collectionView?.addGestureRecognizer(self.longPressGesture)
    collectionView?.addGestureRecognizer(self.tapGesture)
}
我的点击功能:

@objc func handleTapGesture(gesture: UITapGestureRecognizer) {
    self.performSegue(withIdentifier: "displayImage", sender: self)
}
还有我的长按功能:

@objc func handleTapGesture(gesture: UITapGestureRecognizer) {
    self.performSegue(withIdentifier: "displayImage", sender: self)
}
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
    switch(gesture.state) {
    case .began:
        print("begin hold!")
        guard let selectedIndexPath = self.collectionView?.indexPathForItem(at: gesture.location(in: self.collectionView)) else {
            break
        }
        self.collectionView?.beginInteractiveMovementForItem(at: selectedIndexPath)
    case .changed:
        print("change hold!")
        self.collectionView?.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
    case .ended:
        print("end hold!")
        UIView.performWithoutAnimation {
            self.collectionView?.endInteractiveMovement()
        }
    default:
        print("cancel hold!")
        self.collectionView?.cancelInteractiveMovement()
    }
}
当撞车时,我会受伤

libc++abi.dylib:以类型为的未捕获异常终止 N例外

这似乎发生在嵌入func handlelongshirpose()的.changed大小写中


有什么想法吗?非常感谢。

经过几个小时的努力,我终于找到了问题的原因。它与手势无关,而是与集合视图及其数据有关。在VIEWWILLEXPENCE()中,我通过将缩略图重新添加到数组来更新视图:

self.thumbnails.removeAll()
    for i in 0 ..< self.images.count {
        ... adding new (updating) thumbnails ...
}
self.thumbnails.removeAll()
对于0中的i..

但由于某种原因,我忘记调用thumbnails.removeAll(),导致数据被复制,同时没有在集合视图上调用reloadData(),导致它崩溃。

向集合视图添加手势听起来不是个好主意。此外,您甚至不显示如何从集合视图中调用call HandleLong手势。这似乎是使用UILongPressGestureRecognitor处理集合视图单元格重新排列的标准方法。使用UIAPTestureRecognizer的原因是UILongPressGestureRecognizer与UICollectionViewCell didSelectItemAtIndexPath之间存在冲突。函数调用是在集合视图中包含手势识别器后进行的,该集合视图在viewDidLoad()中完成。