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)
}
}
}