Swift3.0, Transition

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容