Junior — Middle
Navigation Controllerдеги экрандар арасында өтүүдө өзгөчөлөнгөн анимациялык өтүүнү кантип ишке ашырууга болот?
sobes.tech AI
AIден жооп
UINavigationController ичинде экрандар арасында өзгөчө анимациялык өтүүнү ишке ашыруу үчүн UIViewControllerAnimatedTransitioning жана UINavigationControllerDelegate протоколдорун колдонуш керек.
-
UIViewControllerAnimatedTransitioningпротоколун ишке ашыратган класс түзүңүз жана анимациянын логикасын төмөнкү методдордо сүрөттөңүз:transitionDuration(using:)— анимациянын узактыгыanimateTransition(using:)— өзү өтүү
-
UINavigationController делегатын
UINavigationControllerDelegateишке ашырган объектке белгилеп, төмөнкү методдо:func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { // сиздин анимация объектиңузду кайтарыңыз return customAnimator }
Жөнөкөй адаптацияланган анимационердин мисалы:
class CustomAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toView = transitionContext.view(forKey: .to) else {
transitionContext.completeTransition(false)
return
}
let container = transitionContext.containerView
toView.alpha = 0
container.addSubview(toView)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
toView.alpha = 1
}) { finished in
transitionContext.completeTransition(finished)
}
}
}