Swift3.0, Transition

2017-05-16  本文已影响50人  FaiChou

Animator

import Foundation
import UIKit

class TransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning {
  let animationDuration = 0.5
  
  func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return animationDuration
  }
  func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    transitionContext.containerView.backgroundColor = .white
    let toViewController = transitionContext.viewController(forKey: .to)!
    toViewController.view.frame = transitionContext.finalFrame(for: toViewController).offsetBy(dx: 0, dy: 300)
    transitionContext.containerView.addSubview(toViewController.view)
    
    UIView.animate(
      withDuration: animationDuration,
      delay: 0,
      usingSpringWithDamping: 0.2,
      initialSpringVelocity: 0.1,
      options: .curveLinear,
      animations: {
        toViewController.view.frame =
          transitionContext.finalFrame(for: toViewController)
    }) { (finished) in
      transitionContext.completeTransition(true)
    }
  }
}

Usage

import UIKit

class ViewController: UIViewController, UIViewControllerTransitioningDelegate {

  let animator = TransitionAnimator()
  let pushButton: UIButton = {
    let b = UIButton(type: .custom)
    b.setTitle("PUSH", for: .normal)
    b.setTitleColor(.blue, for: .normal)
    b.addTarget(self, action: #selector(pushToB), for: .touchUpInside)
    return b
  }()
  func pushToB() {
    let bVC = BViewController()
    bVC.dismissCall = {
      self.dismiss(animated: true, completion: nil)
    }
    bVC.transitioningDelegate = self
    self.present(bVC, animated: true, completion: nil)
  }
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor(red:0.76, green:0.55, blue:0.86, alpha:1.00)
    view.addSubview(pushButton)
  }
  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    pushButton.frame = CGRect(x: 100, y: 100, width: 100, height: 20)
  }
  func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return animator
  }
}
class BViewController: UIViewController {
  typealias VoidCallBack = ()->()
  var dismissCall: VoidCallBack?
  let dismissButton: UIButton = {
    let b = UIButton(type: .custom)
    b.setTitle("DISMISS", for: .normal)
    b.setTitleColor(.blue, for: .normal)
    b.addTarget(self, action: #selector(dismissB), for: .touchUpInside)
    return b
  }()
  func dismissB() {
    if dismissCall != nil {
      dismissCall!()
    }
  }
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor(red:0.29, green:0.88, blue:0.24, alpha:1.00)
    view.addSubview(dismissButton)
  }
  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    dismissButton.frame = CGRect(x: 100, y: 100, width: 100, height: 20)
  }
}

上一篇 下一篇

猜你喜欢

热点阅读