[swift] Swift Presentr와 함께 사용하는 시간 일정 관리 기능 구현 방법

Swift Presentr는 Swift로 작성된 iOS 앱에서 Alert 및 Action Sheet와 같은 팝업 창을 쉽게 구현할 수 있도록 도와주는 라이브러리입니다. 이 라이브러리를 사용하여 시간 일정 관리 기능을 구현하는 방법을 알아보겠습니다.

1. Presentr 라이브러리 설치

먼저, Presentr 라이브러리를 설치해야 합니다. Cocoapods를 사용한다면 Podfile에 다음과 같은 내용을 추가합니다.

pod 'Presentr'

그리고 터미널에서 pod install 명령을 실행하여 라이브러리를 설치합니다.

2. 팝업 창 디자인 작성

Presentr를 사용하여 팝업 창을 구현하기 전에, 팝업 창의 디자인을 먼저 작성해야 합니다. 일반적으로 UIView 또는 UIViewController를 사용하여 커스텀 팝업 창을 작성할 수 있습니다. 디자인 작업은 개발자의 요구에 따라 다르므로 자세한 설명은 생략하겠습니다.

3. Presentr 객체 생성

팝업 창을 띄우기 위해 Presentr 객체를 생성해야 합니다. Presentr을 사용하여 팝업 창을 구현할 때는 역방향 트랜지션을 사용하는 것이 좋습니다. Presentr 객체를 생성할 때 다음과 같은 옵션을 설정할 수 있습니다.

let presenter: Presentr = {
    let width = ModalSize.custom(size: 300)
    let height = ModalSize.custom(size: 200)
    let center = ModalCenterPosition.customOrigin(origin: CGPoint(x: 0, y: 100))

    let customType = PresentationType.custom(width: width, height: height, center: center)
    let presenter = Presentr(presentationType: customType)
    
    presenter.transitionType = TransitionType.crossDissolve
    presenter.dismissOnSwipe = true

    return presenter
}()

위 코드에서는 팝업 창의 크기를 300x200으로 설정하고, 위치를 (0, 100)으로 지정하였습니다. 또한, crossDissolve 애니메이션을 사용하여 팝업 창을 띄웠을 때 부드러운 효과를 줄 수 있습니다.

4. 팝업 창 띄우기

Presentr 객체를 사용하여 팝업 창을 띄울 수 있습니다. UIViewController를 상속받은 파일 내에서 다음과 같은 코드를 작성하여 팝업 창을 띄울 수 있습니다.

let popupViewController = PopupViewController() // 팝업 창에 표시할 View Controller 객체 생성

customPresenterViewController.customPresentViewController(
    customPresenterViewController.presenter,
    viewController: popupViewController,
    animated: true,
    completion: nil
)

위 코드에서는 PopupViewController라는 커스텀 View Controller를 생성하여 팝업 창에 표시하고 있습니다. 생성한 Presentr 객체를 사용하여 presentrViewController 메소드를 호출하면 팝업 창이 화면에 표시됩니다.

5. 시간 일정 관리 기능 추가

팝업 창에 시간 일정 관리 기능을 추가해 보겠습니다. 예를 들어, 팝업 창 내부에 일정 목록을 표시하는 UITableView를 추가하는 경우 다음과 같은 코드를 작성할 수 있습니다.

class PopupViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let tableView = UITableView()  // 일정 목록을 표시하기 위한 UITableView
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // UITableView 설정
        tableView.delegate = self
        tableView.dataSource = self
        
        // 팝업 창에 UITableView 추가
        self.view.addSubview(tableView)
    }
    
    // UITableViewDataSource 프로토콜 구현
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 일정 목록의 개수 반환
        return scheduleList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        
        // 일정 목록을 테이블 뷰 셀에 표시
        cell.textLabel?.text = scheduleList[indexPath.row]
        
        return cell
    }
}

위 코드에서는 UITableViewDelegateUITableViewDataSource 프로토콜을 구현하여 일정 목록을 표시하고 있습니다. numberOfRowsInSection 메소드에서 일정 목록의 개수를 반환하고, cellForRowAt 메소드에서 각 셀에 일정을 표시하고 있습니다.

이제 팝업 창에 일정 목록이 표시되는 기능이 추가된 것입니다.

결론

Swift Presentr를 사용하여 시간 일정 관리 기능을 구현하는 방법을 알아보았습니다. Presentr 라이브러리를 설치하고 팝업 창의 디자인을 작성한 후, Presentr 객체를 생성하여 팝업 창을 띄울 수 있습니다. 시간 일정 관리 기능을 추가하기 위해 팝업 창 내부에 UITableView를 추가하고 필요한 기능을 구현하면 됩니다. Swift Presentr를 사용하면 간편하게 팝업 창을 구현할 수 있으며, 시간 일정 관리와 같은 다양한 기능을 추가할 수 있습니다.

참고 자료