Search code examples
swiftanimationuicollectionviewlong-pressuicontextmenuconfiguration

How to disable long-press animation of context menu for CollectionView iOS Swift?


My ViewController have the three UICollectionView for all the UICollectionView I did create contextMenu, but for the last UICollectionView I am don't want to show the menu and for <return UIContextMenuConfiguration(identifier: nil, previewProvider: nil)> all work well but for this UICollectionview animation of long-press didn't disabled.

How to disable the animation of the long press for the last UICollectionView? It does be easy but I can't delete the default case.

Example of my code:

func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        
        switch collectionView {
        case favoriteCollectionView:
             return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) {...}
        case allCollectionView:
             return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) {...}
        default:
              // This menu empty and doesn't show but the animation of long pressed not disabled how to fix this?
             return UIContextMenuConfiguration(identifier: nil, previewProvider: nil)
        }
    }

Solution

  • Pass return nil insted of return UIContextMenuConfiguration(identifier: nil, previewProvider: nil)

    func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    
        switch collectionView {
        case favoriteCollectionView:
            return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) {...}
        case allCollectionView:
            return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) {...}
        default:
            return nil //<-- HERE
        }
    }