[swift] IGListKit에서 셀의 드래그 앤 드롭 동작 설정하는 방법

IGListKit은 콜렉션 뷰의 데이터 소스 및 레이아웃을 관리하는 라이브러리입니다. 이 라이브러리를 사용하여 사용자가 셀을 드래그 앤 드롭 할 수 있는 동작을 설정할 수 있습니다. 이번 글에서는 IGListKit을 사용하여 셀의 드래그 앤 드롭 동작을 설정하는 방법에 대해 알아보겠습니다.

IGListKit에서 셀의 드래그 앤 드롭 동작을 설정하려면 다음 단계를 따르세요.

단계 1: IGListBindingSectionController 구현

첫 번째 단계로, IGListBindingSectionController를 구현해야 합니다. IGListBindingSectionController는 셀과 데이터 모델을 연결하고 이벤트를 처리하는 역할을 합니다.

class MySectionController: IGListBindingSectionController<ListDiffable> {
    // ...
}

단계 2: 드래그 앤 드롭 동작 활성화

IGListBindingSectionController에서 드래그 앤 드롭 동작을 활성화하려면 shouldHandleLongPressGesture 메서드를 오버라이드하고 true를 반환해야 합니다.

class MySectionController: IGListBindingSectionController<ListDiffable> {
    // ...

    override func shouldHandleLongPressGesture() -> Bool {
        return true
    }
}

단계 3: 드롭 대상 지정

특정 셀이 드롭을 받을 수 있는 대상으로 지정하려면 targetObjects(forDrag coordinator: UIDragCoordinator) 메서드를 오버라이드하고 드롭 대상으로 지정할 데이터 모델을 반환해야 합니다.

class MySectionController: IGListBindingSectionController<ListDiffable> {
    // ...

    override func targetObjects(forDrag coordinator: UIDragCoordinator) -> [Any] {
        guard let droppableItem = object else {
            return []
        }
        
        return [droppableItem]
    }
}

단계 4: 드롭 동작 처리

드롭된 데이터를 처리하려면 didDrop(with coordinator: UIDropCoordinator) 메서드를 오버라이드하고 적절한 동작을 구현해야 합니다.

class MySectionController: IGListBindingSectionController<ListDiffable> {
    // ...

    override func didDrop(with coordinator: UIDropCoordinator) {
        // 드롭된 데이터 처리 코드 구현
    }
}

이제 위의 단계를 따라서 IGListKit에서 셀의 드래그 앤 드롭 동작을 설정할 수 있습니다. IGListKit은 셀의 드래그 앤 드롭 이벤트를 처리하고 해당 동작을 수행하기 위한 메서드를 제공합니다.

참고: IGListKit Documentation