Giter Site home page Giter Site logo

lilianerhan / lazytransitions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from serp1412/lazytransitions

0.0 1.0 0.0 13.01 MB

Lazy pop and dismiss like in the Facebook, Instagram or Twitter apps.

License: MIT License

Ruby 0.56% Swift 84.21% Objective-C 10.07% Shell 5.17%

lazytransitions's Introduction

LazyTransitions

Twitter

A simple framework that allows you to create similar lazy pops and dismisses like in the Facebook, Instagram or Twitter apps.

LazyTransitions

Installation

CocoaPods

Add the following line to your PodFile:

pod 'LazyTransitions' , :git => 'https://github.com/serp1412/LazyTransitions.git'

Usage

The simplest way to use this framework is to take advantage of UniversalTransitionsHandler class.

You can just give it the views in your view controller that will trigger a transition when the user swipes on them. It could be a simple static view. Or a scroll view that will trigger the transition when it reaches the edges of it's content.

  • Import the framework
import LazyTransitions
  • Create an instance of UniversalTransitionsHandler
let transitioner = UniversalTransitionsHandler()
  • Pass your transition views (views that will trigger a transition when user pans on them) to the handler
transitioner.addTransition(for: view)
// or
transitioner.addTransition(for: scrollView)
  • In the beginTransitionAction trigger your transition (dismiss or pop)
transitioner.beginTransitionAction = { [weak self] _ in
    self?.dismiss(animated: true, completion: nil)
}
  • In your transitioning delegate methods pass the animator and interactor from the transition handler
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    // ... pass the animator
    return transitioner.animator
}
    
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
    // ... pass the interactor
    return transitioner.interactor
}

Example

Here's some sample code on how to use LazyTransitions in your project.

// first of all import LazyTransitions
import LazyTransitions

class MyVC : UIViewController {
    fileprivate let transitioner = UniversalTransitionsHandler()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // add the main view to your transition handler
        transitioner.addTransition(for: view)
        
        // trigger the transition in beginTransitionAction
        transitioner.beginTransitionAction = { [weak self] _ in
            // for dismiss
            self?.dismiss(animated: true, completion: nil)
            // or for pop
            _ = self?.navigationController?.popViewController(animated: true)
        }
        
        // FOR DISMISS
        // become your delegate for custom view controller transitioning
        transitioningDelegate = self
        
        // or FOR POP
        // become the delegate for your navigation controller
        navigationController.delegate = self
    }
}

// FOR DISMISS
// in the view controller's transitioning methods...
extension MyVC : UIViewControllerTransitioningDelegate {
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        // ... pass the animator
        return transitioner.animator
    }
    
    func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        // ... pass the interactor
        return transitioner.interactor
    }
}

// FOR POP
// in the navigation controller's delegate methods
extension MyVC : UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationControllerOperation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        guard operation == .pop else { return nil }
        // ... pass the animator if the operation is pop
        return transitioner.animator
    }
    
    func navigationController(_ navigationController: UINavigationController, 
                              interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        // ... pass the interactor
        return transitioner.interactor
    }
}

Usage Tips

To add the a bouncy effect when user scrolls with inertia and the UIScrollView reaches its edges, do the following:

// become the delegate of your UIScrollView
scrollView.delegate = self

// implement the scrollViewDidScroll() delegate method
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    // forward it to the transitioner
    transitioner.didScroll(scrollView)
}

To achieve the standard pop animation of iOS, use the provided PopAnimator when initializing the UniversalTransitionsHandler

let transitioner = UniversalTransitionsHandler(animator: PopAnimator(orientation: .leftToRight))

You can limit the allowed transition orientations by setting them like this:

transitioner.allowedOrientations = [.leftToRight, .topToBottom, .bottomToTop]

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.