[swift] Swift IGListKit을 사용한 셀 드래그 앤 드롭하기

이번 튜토리얼에서는 Swift와 IGListKit을 사용하여 셀 드래그 앤 드롭 기능을 구현하는 방법을 알아보겠습니다.

IGListKit 소개

IGListKit은 Instagram에서 개발된 iOS용 리스트 관리 프레임워크입니다. IGListKit은 UICollectionView를 기반으로 하며, 매우 효율적인 리스트 처리를 제공합니다. 또한, 셀 드래그 앤 드롭과 같은 기능도 내장하고 있어서 사용하기 편리합니다.

IGListKit 설치

pod 'IGListKit'

위 코드를 Podfile에 추가하고 pod install 명령어를 실행하여 IGListKit을 설치하세요.

셀 드래그 앤 드롭 구현하기

셀 드래그 앤 드롭 기능을 구현하기 위해 IGListCollectionContextbeginInteractiveMovementForItem(at:point:) 메서드와 IGListCollectionContextendInteractiveMovement() 메서드를 사용합니다. 먼저, 드래그 가능한 셀을 만들기 위해 IGListBindable 프로토콜을 구현해야 합니다.

class DraggableCell: UICollectionViewCell, IGListBindable {
    // 셀 내용 구현
    // ...
    
    // IGListBindable 프로토콜 구현
    
    func bindViewModel(_ viewModel: Any) {
        // 뷰모델 데이터를 셀에 바인딩
        // ...
    }
}

다음으로, 드래그 대상 셀에 UICollectionViewDragDelegate 프로토콜을 구현하세요.

class DraggableCell: UICollectionViewCell, IGListBindable, UICollectionViewDragDelegate {
    // 셀 내용 구현
    // ...
    
    // IGListBindable 프로토콜 구현
    
    // UICollectionViewDragDelegate 프로토콜 구현
    
    func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        // 드래그할 아이템을 생성하고 반환
        // ...
    }
}

마지막으로, 드롭 대상 셀에 UICollectionViewDropDelegate 프로토콜을 구현하세요.

class DroppableCell: UICollectionViewCell, IGListBindable, UICollectionViewDropDelegate {
    // 셀 내용 구현
    // ...
    
    // IGListBindable 프로토콜 구현
    
    // UICollectionViewDropDelegate 프로토콜 구현
    
    func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
        // 드롭할 수 있는 세션인지 여부 반환
        // ...
    }
    
    func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
        // 드롭 이벤트 처리
        // ...
    }
}

위의 코드를 참고하여 셀의 드래그 앤 드롭 기능을 구현할 수 있습니다.

결론

이렇게 Swift와 IGListKit을 사용하여 셀 드래그 앤 드롭 기능을 구현하는 방법을 살펴보았습니다. IGListKit은 높은 성능과 편리한 리스트 처리를 제공하기 때문에, 복잡한 리스트 화면을 개발할 때 유용하게 사용할 수 있습니다. IGListKit의 다양한 기능을 활용하여 앱 개발을 보다 효율적으로 진행해보세요.

참고 자료