Transitioning

👋 들어가기 전
오랜만에 돌아왔다..

오랜만에 네이버 부스트캠프를 오랜만에 회고하면서
마지막 프로젝트에서 미쳐 학습하지 못한 내용이 없지 않을까 싶어서 봤더니
현준님께서 만들었던 히어로 애니메이션을 보고
오늘의 주제를 정했다.
iOS앱에서 화면 전환은 크게 2가지가 있다.
네비게이션을 통해 다음 화면으로 들어가거나, modal을 통해 화면에 나타나는 방식이다.
기본적으로 제공해주는 전환 애니메이션도 매우 훌륭하지만
항상 커스텀을 대비해야하는건 Front 개발자로서 피할 수 없는 여정이다.
그리하여 오늘은 "커스텀 화면 전환" 이다.
먼저 Xcode에 오늘의 키워드 Transitioning을 검색하니
이렇게 나오는데 오늘은 Transitioning관련은 3번째와 4번째를 학습해보자.
다른 부분은 필요할 때 다시 이 포스팅에 이어나가겠다.

🚀 UIViewControllerAnimatedTransitioning
⭐️ 정의
이 프르토콜은 뷰컨트롤러가 프렌젠트될 때 또는 디스미스 될 때에 필요한 에니메이션을
직접 구현하는 프로토콜이다.
이번 학습의 핵심 객체라고 할 수 있다.
🔥 구성요소
1. transitionDuration
애니메이션 지속 시간을 지정한다.
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
}
2. animationEnded
애니메이션이 완전히 끝난 뒤 호출, 보통 데이터 정리등 클린을 위한 작업을 진행
func animationEnded(_ transitionCompleted: Bool) {
print("✅ 애니메이션 종료")
}
3. animateTransition
실질적으로 애니메이션을 구현하는 곳
func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
4. interruptibleAnimator
func interruptibleAnimator(using transitionContext: any UIViewControllerContextTransitioning) -> any UIViewImplicitlyAnimating
시스템이 애니메이션을 중단하거나 / 재시작할 수 있는 UIViewPropertyAnimator를 반환
예를 들어 스와이프 제스처로 화면 전환 시, 애니메이션을 할 수 있게 만듬
즉, 3번 animateTransition은 한번 실행되고 반환되고, 위 객체는 인터렉티브 대응이 가능함.
🧹 정리
실질적인 애니메이션 시간, 내용등을 구성한다.
🏋️ UIViewControllerTransitionDelegate
⭐️ 정의
고정된 길이 또는 인터랙티브한 전환을 관리하는 메서드 집합
🔥 구성요소
1. animationController(forPresented:)
Presented 시점에 사용될 애니메이션 지정
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomPresentAnimation()
}
2. animationController(forDismissed:)
Dissmissed 시점에 사용될 애니메이션 지정
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
3. interationControllerForPresentation
제스처등을 이용한 Presnt 시점 애니메이션 지정
func interactionControllerForPresentation(using animator: any UIViewControllerAnimatedTransitioning) -> (any UIViewControllerInteractiveTransitioning)? {
}
4. interationControllerForDismissal
제스처등을 이용한 Dismiss 애니메이션 지정
func interactionControllerForDismissal(using animator: any UIViewControllerAnimatedTransitioning) -> (any UIViewControllerInteractiveTransitioning)? {
}
5. presentationController
전체 Present 과정을 제어
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
}
🧹 정리
화면 진입 / 또는 제거 때 어떤 애니메이션을 쓸 지를 정하고
과정을 제어한다.
🏋️ UIAdaptivePresentationControllerDelegate
⭐️ 정의
앱의 특성 변경과 함께 presentation과 dismiss에 응답하는 방법을 결정하는 메서드들의 집합
🔥 구성요소
1. adaptivePresentationStyle(for controller)
시스템에 기본으로 사용될 style을 변경을 내가 원하는 것으로 강제 사용
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
.overFullScreen
}
2. adaptivePresentationStyle(for controller, traitCollection: UITraitCollection)
traitCollection과 함께 어떤 style을 결정할 때 사용
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
if traitCollection.layoutDirection == .leftToRight {
return .popover
} else {
return .formSheet
}
}
3. presentationController(_ controller, viewControllerForAdaptivePresentationStyle)
특정 style로 style이 적용될 때 보여질 뷰컨을 지정한다. 보통 nil로하며, 목적지 뷰컨을 보여준다.
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
}
4. presentationController(_ presentationController, willPresentWithAdaptiveStyle, transitionCoordinator)
style 적용 직전 호출, 애니메이션 연동에 사용
func presentationController(_ presentationController: UIPresentationController, willPresentWithAdaptiveStyle style: UIModalPresentationStyle, transitionCoordinator: (any UIViewControllerTransitionCoordinator)?) {
}
5. presentationControllerDidAttemptToDismiss(_ presentationController)
닫으려고 시도하다 실패했을 때 호출
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
}
7. presentationControllerShouldDismiss(_ presentationController)
닫혀도 되는 지 결정 true이면 반환, false면 닫히지 못함
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
}
7. presentationControllerDidDismiss(_ presentationController)
완전히 닫힐 때 호출됨
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
}
8. presentationControllerWillDismiss(_ presentationController)
모달을 닫으려 할 때, 애니메이션 시작 전에 호출됨
func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {
}
9. presentationController(_ presentationController, prepare)
스타일 적용 준비 시, 추가 설정을 하기 위해 사용
func presentationController(_ presentationController: UIPresentationController, prepare adaptivePresentationController: UIPresentationController) {
}
출처
iOS08-Shook/Projects/Features/MainFeature/Sources/Utilities/CollectionViewCellTransitioning.swift at develop · boostcampwm-2024
잘하면 재밌고, 못하면 더 재밌는 실시간 게임 스트리밍! 🌠. Contribute to boostcampwm-2024/iOS08-Shook development by creating an account on GitHub.
github.com
https://developer.apple.com/documentation/uikit/uiviewcontrollertransitioningdelegate
UIViewControllerTransitioningDelegate | Apple Developer Documentation
A set of methods that vend objects used to manage a fixed-length or interactive transition between view controllers.
developer.apple.com
https://developer.apple.com/documentation/uikit/uiadaptivepresentationcontrollerdelegate
UIAdaptivePresentationControllerDelegate | Apple Developer Documentation
A set of methods that, in conjunction with a presentation controller, determine how to respond to trait changes in your app.
developer.apple.com
https://developer.apple.com/documentation/uikit/uiviewcontrolleranimatedtransitioning
UIViewControllerAnimatedTransitioning | Apple Developer Documentation
A set of methods for implementing the animations for a custom view controller transition.
developer.apple.com