A UIKit implementation of a hero image transition between view controllers, using a snapshot-based animation technique.
HeroAnimator conforms to UIViewControllerAnimatedTransitioning and produces a smooth "shared element" transition. It captures a snapshot of the source image view, flies it across the screen to the destination frame using spring physics, and reveals the destination view controller underneath.
- Shared-element hero transition for
UIImageView - Snapshot-based animation (no view hierarchy hacking)
- Spring animation with damping 0.9 and initial velocity 1.6 for a natural feel
- Animates both frame, corner radius, and alpha simultaneously
- Views are tagged (tag
100) for easy source/destination lookup - 0.3 s transition duration
- Swift — UIKit,
UIViewControllerAnimatedTransitioning - UIKit —
UIView.animate(withDuration:usingSpringWithDamping:...)
- Xcode 14+
- iOS 15+
- Add
HeroAnimator.swiftto your project. - Tag the source
UIImageViewand the destinationUIImageViewboth with tag100. - Return a
HeroAnimatorinstance from yourUINavigationControllerDelegateorUIViewControllerTransitioningDelegate:
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return HeroAnimator(imageView: selectedImageView)
}Apache License 2.0 — see LICENSE for details.