Build an interruptible custom VC transition
The custom transition protocol stack.
A delegate vends an animator (UIViewControllerAnimatedTransitioning) for the visuals and an interactive controller (UIViewControllerInteractiveTransitioning) driven by a pan to scrub progress and…
WHAT THIS TESTS This checks deep UIKit knowledge: the protocols behind custom transitions, the clean split between what the animation looks like and how a gesture scrubs it, and the lifecycle calls that keep UIKit's state consistent.
A GOOD ANSWER COVERS You set a transitioningDelegate on the presented view controller (or a navigation controller delegate for push and pop). The delegate returns an animator object conforming to UIViewControllerAnimatedTransitioning. That object implements transitionDuration(using:) and animateTransition(using:), where you read the from and to view controllers and views from the transitioningContext, add views to the context's containerView, run the animation, and crucially call context.completeTransition(!context.transitionWasCancelled) when done. For interactivity, the delegate also returns an interaction controller conforming to UIViewControllerInteractiveTransitioning. The common implementation is UIPercentDrivenInteractiveTransition, which you drive from a pan gesture: as the finger moves you call update(progress), and on release you call finish() or cancel() based on velocity and distance. The animator supplies the visuals; the interaction controller scrubs them.
COMMON WRONG ANSWERS Confusing the two protocols, putting animation code in the interaction controller. Forgetting to call completeTransition, which leaves UIKit in a broken state. Not handling the cancelled case so views end up in the wrong place. Returning an interaction controller even for non-interactive presentations.
LIKELY FOLLOW-UPS How interruptibility uses UIViewPropertyAnimator and UIViewControllerAnimatedTransitioning's interruptibleAnimator(using:) to pause and reverse mid-flight. How to only return the interaction controller while the gesture is active. Differences for presentation versus navigation transitions.
ONE CONCRETE EXAMPLE For swipe-to-dismiss you attach a pan recognizer to the presented controller. When the drag begins you set a flag and the delegate returns your UIPercentDrivenInteractiveTransition. As the user drags down, percentForGesture maps translation to 0 to 1 and you call interactor.update(percent). On release, if percent exceeds 0.4 or velocity is high you call interactor.finish() and the animator completes the dismissal; otherwise interactor.cancel() reverses it. The animator object independently defines the slide-down and fade.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.