Code Monkey home page Code Monkey logo

elastictransition's Introduction

ElasticTransition

Version License Platform

A UIKit custom modal transition that simulates an elastic drag. Written in Swift.

demo

###Special thanks to @taglia3 for developing the [Objective-C version] (https://github.com/taglia3/ElasticTransition-ObjC). Check it out!

Requirements

  • Xcode 7 or higher
  • iOS 8.0 or higher
  • ARC
  • Swift 2.0

Installation

####CocoaPods

use_frameworks!
pod "ElasticTransition"

####Manual

Download(v2.0.1) and add ElasticTransition folder into your project.

Usage

First of all, in your view controller, create an instance of ElasticTransition

var transition = ElasticTransition()
override func viewDidLoad() {
  super.viewDidLoad()

  // customization
  transition.edge = .Left
  transition.sticky = false
  transition.panThreshold = 0.3
  transition.transformType = .TranslateMid
  // ...
}

Use as navigation controller's delegate

Simply assign the transition to your navigation controller's delegate

navigationController?.delegate =transition

Present as modal

In prepareForSegue, assign the transition to be the transitioningDelegate of the destinationViewController. Also, dont forget to set the modalPresentationStyle to .Custom

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  segue.destinationViewController.transitioningDelegate = transition
  segue.destinationViewController.modalPresentationStyle = .Custom
}

In your modal view controller implement the ElasticMenuTransitionDelegate and provide the contentLength

class MenuViewController: UIViewController, ElasticMenuTransitionDelegate {
  var contentLength:CGFloat = 320
  // ...
}
Interactive transition for modal transition

First, construct a pan gesture recognizer

let panGR = UIPanGestureRecognizer(target: self, action: "handlePan:")
view.addGestureRecognizer(panGR)

Then implement your gesture handler and fo the following:

func handlePan(pan:UIPanGestureRecognizer){
  if pan.state == .Began{
    // Here, you can do one of two things
    // 1. show a viewcontroller directly
    let nextViewController = // construct your VC ...
    transition.startInteractiveTransition(self, toViewController: nextViewController, gestureRecognizer: pan)
    // 2. perform a segue
    transition.startInteractiveTransition(self, segueIdentifier: "menu", gestureRecognizer: pan)
  }else{
    transition.updateInteractiveTransition(gestureRecognizer: pan)
  }
}
Interactive transition for dismissing the modal
  1. Implement ElasticMenuTransitionDelegate in your modal view controller and set
  var dismissByBackgroundTouch = true
  var dismissByBackgroundDrag = true
  var dismissByForegroundDrag = true
  1. Or use your own panGestureRecognizer and call dissmissInteractiveTransition in your handler
func handlePan(pan:UIPanGestureRecognizer){
  if pan.state == .Began{
    transition.dissmissInteractiveTransition(self, gestureRecognizer: pan, completion: nil)
  }else{
    transition.updateInteractiveTransition(gestureRecognizer: pan)
  }
}

Todo

  1. Better Guide and Documentation
  2. Testing

Author

Luke Zhao, [email protected]

License

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

elastictransition's People

Contributors

lkzhao avatar wuxiao356 avatar

Watchers

 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.