[swift] Swift IGListKit에서 컬렉션 뷰 셀 스와이프 처리하기

IGListKit은 iOS 앱에서 컬렉션 뷰를 구현하기 위한 강력한 프레임워크입니다. 이 프레임워크를 사용하면 컬렉션 뷰에서 각각의 셀을 쉽게 구현할 수 있습니다. 여기서는 IGListKit을 사용하여 컬렉션 뷰 셀의 스와이프 기능을 구현하는 방법에 대해 알아보겠습니다.

IGListKit 설치하기

IGListKit을 사용하려면 먼저 프로젝트에 IGListKit을 설치해야 합니다. CocoaPods를 사용하여 설치하는 방법은 다음과 같습니다. Podfile에 다음과 같은 내용을 추가하고 pod install 명령어를 실행하세요.

platform :ios, '9.0'
use_frameworks!

target 'YourProjectName' do
  pod 'IGListKit'
end

컬렉션 뷰 셀 스와이프 처리하기

  1. IGListKit에서 컬렉션 뷰 셀을 구현하기 위해 IGListSectionController를 상속하는 새로운 클래스를 만듭니다.

  2. IGListBindingSectionController를 상속하는 경우라면 IGListBindingSectionControllerDelegate 프로토콜을 구현합니다.

  3. IGListBindingSectionController를 사용하지 않는 경우 IGListSectionControllerDelegate 프로토콜을 구현합니다.

  4. 스와이프 기능을 추가하려는 셀의 모델에 플래그 변수를 추가하여 스와이프 상태를 관리할 수 있습니다. 예를 들어, isSwiped라는 불리언 변수를 사용할 수 있습니다.

  5. 컬렉션 뷰 셀의 스와이프 동작을 처리하기 위해 IGListSwipeActionsDelegate 프로토콜을 구현합니다. 이 프로토콜을 구현한 클래스의 인스턴스를 IGListCollectionViewDelegateSwipeToPerformActions를 통해 컬렉션 뷰에 할당합니다.

  6. IGListSectionController에서 스와이프 기능을 구현하기 위해 IGListSwipeActionsDelegate 프로토콜의 메서드 listAdapter(_:trailingSwipeActionsConfigurationForItemAt:for:)을 구현합니다. 이 메서드에서 UISwipeActionsConfiguration을 반환하는데, 여기에 스와이프될 때 실행할 액션을 추가할 수 있습니다.

import UIKit
import IGListKit

class MySectionController: IGListBindingSectionController<MyCell, MyModel>, IGListSwipeActionsDelegate {

  override init() {
    super.init()

    // 스와이프 동작을 처리하기 위해 IGListSwipeActionsDelegate를 구현한 클래스의 인스턴스를 할당합니다.
    self.collectionContext?.delegate = self
  }

  // 스와이프할 때 실행할 액션을 설정합니다.
  func listAdapter(_ listAdapter: IGListAdapter, trailingSwipeActionsConfigurationForItemAt index: Int, for listSectionController: IGListSectionController) -> UISwipeActionsConfiguration? {
    guard let model = self.viewModel?.isSwiped else { return nil }

    if model.isSwiped {
      let resetAction = UIContextualAction(style: .normal, title: "Reset") { (action, view, completion) in
        // 스와이프 상태를 초기화하는 작업을 수행합니다.
        model.isSwiped = false
        completion(true)
      }
      resetAction.backgroundColor = .blue

      let configuration = UISwipeActionsConfiguration(actions: [resetAction])

      return configuration
    } else {
      let swipeAction = UIContextualAction(style: .normal, title: "Swipe") { (action, view, completion) in
        // 스와이프 상태를 변경하는 작업을 수행합니다.
        model.isSwiped = true
        completion(true)
      }
      swipeAction.backgroundColor = .red

      let configuration = UISwipeActionsConfiguration(actions: [swipeAction])

      return configuration
    }
  }
}

  1. 마지막으로, 컬렉션 뷰에서 스와이프 동작을 수행할 수 있도록 IGListCollectionViewDelegateSwipeToPerformActions를 설정합니다. 이를 위해 IGListKit를 사용하는 뷰 컨트롤러에서 IGListAdapterDelegate를 구현하고, listAdapter(_:willBeginDraggingItemAt:) 메서드에서 뷰 컨트롤러의 collectionView에 해당 델리게이트를 할당합니다.
import UIKit
import IGListKit

class MyViewController: UIViewController, IGListAdapterDelegate {

  private lazy var adapter: IGListAdapter = {
    return IGListAdapter(updater: IGListAdapterUpdater(), viewController: self, workingRangeSize: 0)
  }()

  @IBOutlet weak var collectionView: UICollectionView!

  override func viewDidLoad() {
    super.viewDidLoad()

    adapter.collectionView = collectionView
    adapter.dataSource = self
    adapter.delegate = self
  }
  
  //...

  func listAdapter(_ listAdapter: IGListAdapter, willBeginDraggingItemAt indexPath: IndexPath) {
    // 스와이프 동작을 수행할 수 있도록 IGListCollectionViewDelegateSwipeToPerformActions를 설정합니다.
    collectionView.delegate = listAdapter.collectionViewDelegate
  }

  //...
}

이렇게 하면 IGListKit을 사용하여 컬렉션 뷰 셀의 스와이프 기능을 구현할 수 있습니다. IGListKit을 사용하면 컬렉션 뷰를 효율적으로 구현할 수 있으며, 사용자에게 좋은 사용자 경험을 제공할 수 있게 됩니다.

참고 자료