Swift Presentr은 iOS에서 사용할 수 있는 강력한 뷰 컨트롤러 프레젠터 라이브러리입니다. 이 라이브러리를 사용하면 간편하게 커스텀 애니메이션과 함께 뷰 컨트롤러를 present 할 수 있습니다.
설치
CocoaPods를 사용하여 Swift Presentr을 설치할 수 있습니다. Podfile에 다음과 같이 추가합니다:
pod 'SwiftPresentr'
그런 다음 터미널에서 다음 명령어를 실행하여 종속성을 설치합니다:
pod install
사용법
Swift Presentr을 사용하기 위해 뷰 컨트롤러를 present 할 때 Presentr
객체를 인스턴스화해야 합니다. 다음은 기본 사용법의 예시입니다:
import SwiftPresentr
class ViewController: UIViewController {
let presenter = Presentr(presentationType: .bottomHalf)
// Present 버튼 클릭 시 동작하는 메서드
@IBAction func presentButtonTapped(_ sender: UIButton) {
let nextViewController = NextViewController()
customPresenter.present(nextViewController, animated: true, completion: nil)
}
}
위 예시에서 Presentr
객체는 presentationType
매개변수로 .bottomHalf
를 받아옵니다. 이는 뷰 컨트롤러를 하단 절반부터 현재 화면에 present하는 것을 의미합니다.
Presentr 객체를 만든 후 present()
메서드를 호출하여 뷰 컨트롤러를 present 할 수 있습니다. 위 예시에서는 Present 버튼이 클릭되었을 때 presentButtonTapped()
메서드가 호출되면 다음 뷰 컨트롤러인 NextViewController
를 present합니다.
커스텀 애니메이션
Swift Presentr은 커스텀 애니메이션을 추가할 수 있는 기능을 제공합니다. 다음은 커스텀 애니메이션이 적용된 예시입니다:
class CustomPresenter: Presentr {
override func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return MyCustomTransitionAnimation()
}
override func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return MyCustomDismissAnimation()
}
}
let customPresenter = CustomPresenter()
위 예시에서 CustomPresenter
클래스는 Presentr
을 상속받은 커스텀 presentr입니다. animationController()
메서드를 오버라이드하여 커스텀 트랜지션 애니메이션 객체를 반환합니다. 이러한 커스텀 애니메이션을 사용하여 뷰 컨트롤러를 present하거나 dismiss할 때 애니메이션 효과를 주어 사용자 경험을 향상시킬 수 있습니다.