Code Monkey home page Code Monkey logo

lazytransitions's Introduction

LazyTransitions

Platform: iOS 8+ Language: Swift 3 Cocoapods Carthage Compatible License: BSD Twitter: serp1412

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'

Carthage

Add the following line to your Cartfile

github "serp1412/LazyTransitions"

Usage

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

  • Import the framework
import LazyTransitions
  • Create an instance of LazyTransitioner
let transitioner = LazyTransitioner()
  • Pass your transition views (views that will trigger a transition when user pans on them) to the transitioner
transitioner.addTransition(forView: view)
// or
transitioner.addTransition(forScrollView: scrollView)
  • In the triggerTransitionAction trigger your transition (dismiss or pop)
transitioner.triggerTransitionAction = { [weak self] _ in
    self?.dismiss(animated: true, completion: nil)
}
  • Set your transitioner as the transitioning delegate.

For Dismiss:

transitioningDelegate = transitioner

For Pop:

navigationController?.delegate = transitioner

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 = LazyTransitioner()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // add the main view to your transitioner
        transitioner.addTransition(forView: view)
        
        // trigger the transition in triggerTransitionAction
        transitioner.triggerTransitionAction = { [weak self] _ in
            // for dismiss
            self?.dismiss(animated: true, completion: nil)
            // or for pop
            _ = self?.navigationController?.popViewController(animated: true)
        }
        
        // FOR DISMISS
        // set transitioner as the delegate for custom view controller transitioning
        transitioningDelegate = transitioner
        
        // or FOR POP
        // set transitioner as the delegate for your navigation controller
        navigationController.delegate = transitioner
    }
}

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 LazyTransitioner
let transitioner = LazyTransitioner(animator: PopAnimator(orientation: .leftToRight))
  • You can limit the allowed transition orientations by setting them like this:
transitioner.allowedOrientations = [.leftToRight, .topToBottom, .bottomToTop]
  • In case you need to assign something else as the transitioning delegate, then you can simply forward the animator and interactor properties of your transitioner inside the delegate methods.

Like so in case of dismiss:

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    // ... pass the animator
    return transitioner.animator
}
    
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
    // ... pass the interactor
    return transitioner.interactor
}

lazytransitions's People

Contributors

serp1412 avatar s2dentik avatar

Stargazers

MohsinAli avatar

Watchers

James Cloos avatar MohsinAli avatar  avatar

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.