登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

ElasticTransition

来自: code4app 分类: iOS精品源码 上传时间: 2016-3-15 10:27:12
Tag:animation animation

项目介绍:

ElasticTransition (Swift 3)

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

demo

Thanks to Matt Garnett (@c-o-l-o-r) for converting ElasticTransition to Swift 3

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

Requirements

  • Xcode 8 or higher

  • iOS 8.0 or higher

  • Swift 3.0

Installation

CocoaPods

use_frameworks!
pod "ElasticTransition"

Usage

Simple

import ElasticTransition
// make your view controller a subclass of ElasticModalViewController
// present it as normal
class YourModalViewController:ElasticModalViewController{ 
  // ... 
}

class RootViewController:UIViewController{
  // ...
  @IBAction func modalBtnTouched(sender: AnyObject) {
    performSegueWithIdentifier("modalSegueIdentifier", sender: self)

    // or if you want to do customization ---------------------
    let modalViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("modalViewControllerIdentifier") as! YourModalViewController
    // customization:
    modalViewController.modalTransition.edge = .Left
    modalViewController.modalTransition.radiusFactor = 0.3
    // ...

    presentViewController(modalViewController, animated: true, completion: nil)
  }
}

Attributes you can set:

  // screen edge of the transition
  public var edge:Edge
  // animation stiffness - determines the speed of the animation
  public var stiffness:CGFloat = 0.2
  // animation damping - determines the bounciness of the animation 
  public var damping:CGFloat = 0.2
  // Background view transform
  public var transformType:ElasticTransitionBackgroundTransform = .TranslateMid
  // The curvature of the elastic edge.
  public var radiusFactor:CGFloat = 0.5
  /**
   Determines whether or not the view edge will stick to
   the initial position when dragged.
   **Only effective when doing a interactive transition**
   */
  public var sticky:Bool = true
  /**
   The initial position of the simulated drag when static animation is performed
   i.e. The static animation will behave like user is dragging from this point
   **Only effective when doing a static transition**
   */
  public var startingPoint:CGPoint?
  /**
   The background color of the container when doing the transition
   */
  public var containerColor:UIColor = UIColor(red: 152/255, green: 174/255, blue: 196/255, alpha: 1.0)
  /**
   The color of the overlay when doing the transition
   */
  public var overlayColor:UIColor = UIColor(red: 152/255, green: 174/255, blue: 196/255, alpha: 0.5)
  /**
   Whether or not to display the shadow. Will decrease performance.
   */
  public var showShadow:Bool = false
  /**
   The shadow color of the container when doing the transition
   */
  public var shadowColor:UIColor = UIColor(red: 100/255, green: 122/255, blue: 144/255, alpha: 1.0)
  /**
   The shadow radius of the container when doing the transition
   */
  public var shadowRadius:CGFloat = 50

Advance Usage

This work with any view controller.

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

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

  // customization
  transition.edge = .Left 
  transition.sticky = false
  // etc
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  segue.destinationViewController.transitioningDelegate = transition
  segue.destinationViewController.modalPresentationStyle = .Custom
}

(Optional) 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)
  }
}

Author

Luke Zhao, me@lkzhao.com

License

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

code4app

相关源码推荐:

我来说两句
*滑动验证:
所有评论(10)
ovbm0526 2016-4-7 09:39:24
感谢分享,Code4App有你更精彩
回复
Want5221 2016-4-7 10:03:54
楼主用心了,内容非常精彩。
回复
invest 2016-4-7 10:06:09
精华内容,楼主V5!
回复
Want5221 2016-4-7 10:07:33
我只是路过打酱油的。
回复
bboy@zj 2016-4-12 12:35:47
学习学习!
回复
hyan 2016-4-13 10:49:43
楼主用心了,内容非常精彩。
回复
thank1992you 2016-4-18 09:51:21
强烈支持楼主ing……
回复
nxiak 2016-4-18 14:43:53
感谢分享,Code4App有你更精彩:)
回复
all. 2016-4-21 15:39:48
感谢分享,Code4App有你更精彩
回复
12下一页
提取码:  下载次数:23 状态:已购或VIP 售价:0(原价:10)金钱 下载权限:初级码农 
1563 1 23
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542

如何获得代码达人称号?

代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表