Giter Site home page Giter Site logo

zoomtransitionanimator's Introduction

ZoomTransitionAnimator

CI Status Version License Platform

ZoomTransitionAnimator is a transition animator between view controllers. It zooms the whole scene together, not just the cell or image view you selected. Multi-layer zooming (zoom-in from view controller A to B and then to C) is supported. Although not an interactive transition animator, you can use it with UIPanGestureRecognizer. As long as you return the correct frame of the target view in absolute space (UIScreen.main.coordinateSpace), the animator will find it and zoom out from there.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

ZoomTransitionAnimator is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'ZoomTransitionAnimator'

Usage

  1. Initialize and return a ZoomTransitionAnimator object in your UINavigationControllerDelegate object.

     extension NavigationViewController: UINavigationControllerDelegate {
         func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
             switch (fromVC, toVC) {
             case (let zoomSourceVC as ZoomTransitionSource, let zoomDestinationVC as ZoomTransitionDestination):
                 guard let operation = ZoomTransitionOperation(rawValue: operation.rawValue) else { return nil }
                 return ZoomTransitionAnimator(duration: 0.45, damping: 0.75, source: zoomSourceVC, destination: zoomDestinationVC, operation: operation)
             default:
                 return nil
             }
         }
     }
    
  2. Apply both ZoomTransitionSource and ZoomTransitionDestination protocols to your ParentViewController. Or if you only want the zoom animation to appear in only one direction, choose one of them to apply.

     extension ParentViewController: ZoomTransitionSource {
         func zoomTransitionAnimator(_ animator: ZoomTransitionAnimator, targetViewBeginningFrameFor operation: ZoomTransitionOperation) -> CGRect {
             switch operation {
             case .zoomIn:
                 return selectedCellImageViewFrame
             case .zoomOut:
                 fatalError()
             }
         }
     }
    
     extension ParentViewController: ZoomTransitionDestination {
         func zoomTransitionAnimator(_ animator: ZoomTransitionAnimator, targetViewFinalFrameFor operation: ZoomTransitionOperation) -> CGRect
             switch operation {
             case .zoomIn:
                 fatalError()
             case .zoomOut:
                 return selectedCellImageViewFrame
             }
         }
     }
    

Since the ParentViewController is not going to be a zoom-in destination, you could put fatalError() there. These cases were designed for when a ViewController is both zoom-out destination and zoom-in destination. That is, multi-layer zooming.

  1. Apply those protocols to your ChildViewController too.

     extension ChildViewController: ZoomTransitionSource {
         func zoomTransitionAnimator(_ animator: ZoomTransitionAnimator, targetViewBeginningFrameFor operation: ZoomTransitionOperation) -> CGRect {
             switch operation {
             case .zoomIn:
                 fatalError()
             case .zoomOut:
                 return imageViewFrame
             }
         }
     }
    
     extension ChildViewController: ZoomTransitionDestination {
         func zoomTransitionAnimator(_ animator: ZoomTransitionAnimator, targetViewFinalFrameFor operation: ZoomTransitionOperation) -> CGRect
             switch operation {
             case .zoomIn:
                 return imageViewFrame
             case .zoomOut:
                 fatalError()
             }
         }
     }
    

And that's it, really. You don't have to return any UIView, just the frame of it. So if you want to zoom out from the content of a UIScrollView, just calculate the frame with scrollView.contentOffset and scrollView.contentSize. No need to make a snapshot view anymore.

Author

yesleon, [email protected]

License

ZoomTransitionAnimator is available under the MIT license. See the LICENSE file for more info.

zoomtransitionanimator's People

Contributors

yesleon avatar

Stargazers

Drew Pitchford avatar Joshua Chang avatar MarkFly avatar Gordon Tucker avatar Lihsuan Chen avatar Tericky avatar Carabineiro avatar

Watchers

James Cloos avatar Carabineiro avatar  avatar Joshua Chang avatar

Forkers

carabina

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.