ElasticTransition 3.1.1

ElasticTransition 3.1.1

测试已测试
语言语言 SwiftSwift
许可协议 MIT
发布最后发布2016年11月
SwiftSwift 版本3.0
SPM支持 SPM

Luke Zhao 维护。




  • Luke

ElasticTransition (Swift 3)

一个UIKit定制模态过渡,模拟弹性拖动。用Swift编写。

demo

感谢 Matt Garnett (@c-o-l-o-r) 将 ElasticTransition 转换为 Swift 3

同时,特别感谢 @taglia3 开发了 Objective-C 版本。去看看!

要求

  • Xcode 8 或更高版本
  • iOS 8.0 或更高版本
  • Swift 3.0

使用方法

简单

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

可以设置的属性

  // 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


高级使用方法

这与任何视图控制器都兼容。

在 prepareForSegue 中,将过渡赋给 destinationViewController 的 transitioningDelegate,别忘了设置 modalPresentationStyle 为 .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
}

(可选) 在您的模态视图控制器中实现 ElasticMenuTransitionDelegate 并提供内容长度

class MenuViewController: UIViewController, ElasticMenuTransitionDelegate {
  var contentLength:CGFloat = 320
  // ...
}
模态过渡的交互式过渡

首先,构造一个 pan gesture recognizer

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

然后实现您的手势处理程序并执行以下操作:

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)
  }
}
模态的交互式过渡以撤销
  1. 在您的模态视图控制器中实现 ElasticMenuTransitionDelegate 并设置
  var dismissByBackgroundTouch = true
  var dismissByBackgroundDrag = true
  var dismissByForegroundDrag = true
  1. 或者使用您自己的 panGestureRecognizer 并在处理程序中调用 dismissInteractiveTransition
func handlePan(pan:UIPanGestureRecognizer){
  if pan.state == .Began{
    transition.dissmissInteractiveTransition(self, gestureRecognizer: pan, completion: nil)
  }else{
    transition.updateInteractiveTransition(gestureRecognizer: pan)
  }
}

作者

Luke Zhao,[email protected]

许可证

ElasticTransition遵从MIT许可证。有关更多信息,请参阅LICENSE文件。