Since the existing long-press handler will now have two different actions depending on the view controller's isEditing state, it's a wise idea to split the long-press handler into several methods that will be called depending on the desired action. The handler itself will still ensure that a valid cell and index path exist. But after that, it will forward to another method to perform further actions. Update your code as shown to refactor the long-press handler:
@objc func userDidLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) { let tappedPoint = gestureRecognizer.location(in: collectionView) guard let indexPath = collectionView.indexPathForItem(at: tappedPoint), let tappedCell = collectionView.cellForItem(at: indexPath) else { return } if isEditing { beginReorderingForCell(tappedCell, atIndexPath: indexPath, gestureRecognizer: gestureRecognizer) } else { deleteContactForCell(tappedCell, atIndexPath: indexPath) } } func beginReorderingForCell(_ cell: UICollectionViewCell, atIndexPath indexPath: IndexPath, gestureRecognizer: UILongPressGestureRecognizer) { } func deleteContactForCell(_ tappedCell: UICollectionViewCell, atIndexPath indexPath: IndexPath) { // The existing cell deletion code goes here }
First, the isEditing property on the view controller is used to determine what should happen when the user long-presses. If this property is true, beginReorderingForCell(_:atIndexPath:gestureRecognizer:) is called. You will implement this method later to support reordering. If isEditing is false, deleteContactForCell(_:atIndexPath:) is called to perform the delete action you have already implemented.