Iglistkit: Selective Drag and Drop Reorder

Created on 5 Nov 2018  Â·  1Comment  Â·  Source: Instagram/IGListKit

New issue checklist

Is there a way to selectively reorder sections with drag and drop. I mean, I only want to enable drag and drop reorder from section 2 to section 10. Is it possible?
And if yes, can you hint me some advice?
Thanks in advance!

question

Most helpful comment

I assume you mean that you want to prevent items from being dragged any further than those bounds (so that they stay within section 2 to 10 while dragging) and that you use UICollectionView's interactiveMovement.

I've done a similar thing in an app by overriding targetIndexPath(forInteractivelyMovingItem:withPosition:) -> IndexPath inside a subclass of ListCollectionViewLayout (should also work for any other layout, I think)

class RangedReorderFlowLayout: ListCollectionViewLayout {
    public var allowedSections: ClosedRange<Int>? // Set this to 2...10

    override func targetIndexPath(forInteractivelyMovingItem previousIndexPath: IndexPath, withPosition position: CGPoint) -> IndexPath {
        guard let allowedSections = self.allowedSections, else {
            return super.targetIndexPath(forInteractivelyMovingItem: previousIndexPath, withPosition: position)
        }

        if let newIndexPath = collectionView?.indexPathForItem(at: position), allowedSections.contains(newIndexPath.section) {
            return newIndexPath
        }

        return previousIndexPath
    }
}

I don't know if this is the best solution (or an efficient one), but it's working for me. Whenever you drag an item to a section that's not in allowedSections, it simply returns the previous indexPath, resulting in the item not being moved any further.

However, if it's only about enabling drag&drop in sections 2 to 10 (meaning that you can only start an interactive movement within sections 2 to 10, but you can still move any dragged item outside these bounds), you could simply use

// Inside a ListSectionController subclass
override func canMoveItem(at index: Int) -> Bool {
    // Return true if this is a section where you want to enable drag&drop
}

Hope this helps. Let me know if you meant to do something else, maybe I can help you :)

>All comments

I assume you mean that you want to prevent items from being dragged any further than those bounds (so that they stay within section 2 to 10 while dragging) and that you use UICollectionView's interactiveMovement.

I've done a similar thing in an app by overriding targetIndexPath(forInteractivelyMovingItem:withPosition:) -> IndexPath inside a subclass of ListCollectionViewLayout (should also work for any other layout, I think)

class RangedReorderFlowLayout: ListCollectionViewLayout {
    public var allowedSections: ClosedRange<Int>? // Set this to 2...10

    override func targetIndexPath(forInteractivelyMovingItem previousIndexPath: IndexPath, withPosition position: CGPoint) -> IndexPath {
        guard let allowedSections = self.allowedSections, else {
            return super.targetIndexPath(forInteractivelyMovingItem: previousIndexPath, withPosition: position)
        }

        if let newIndexPath = collectionView?.indexPathForItem(at: position), allowedSections.contains(newIndexPath.section) {
            return newIndexPath
        }

        return previousIndexPath
    }
}

I don't know if this is the best solution (or an efficient one), but it's working for me. Whenever you drag an item to a section that's not in allowedSections, it simply returns the previous indexPath, resulting in the item not being moved any further.

However, if it's only about enabling drag&drop in sections 2 to 10 (meaning that you can only start an interactive movement within sections 2 to 10, but you can still move any dragged item outside these bounds), you could simply use

// Inside a ListSectionController subclass
override func canMoveItem(at index: Int) -> Bool {
    // Return true if this is a section where you want to enable drag&drop
}

Hope this helps. Let me know if you meant to do something else, maybe I can help you :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yusuftor picture yusuftor  Â·  25Comments

heumn picture heumn  Â·  27Comments

jessesquires picture jessesquires  Â·  16Comments

jessesquires picture jessesquires  Â·  19Comments

RamblinWreck77 picture RamblinWreck77  Â·  17Comments