Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

基础使用

DianQK edited this page Dec 29, 2015 · 5 revisions

执行一个 Push 的转场

假设现在时从FirstViewController Push 到 SecondViewController ,那么SecondViewController必须服从TRTransition协议,也就是在你的类上加一个var tr_transition: TRNavgationTransitionDelegate?,我需要使用这个属性保留动画实例,当然你也可以用这个来做更多地定制,但是请务必注意,这是一件很危险地事情!!!
当你需要执行 Push 的操作时,只需要调用public func tr_pushViewController(viewcontroller: UIViewController, method: TRPushMethod, completion: (() -> Void)?),基本上就是类似官方的调用方法,关于每个转场方法的参数,transitiontreasury.com 上每个转场动画的第三行就是对应的参数。
示例代码如下:

class OMINViewController: UIViewController, TRTransition {
    
    var tr_transition: TRNavgationTransitionDelegate?

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let view = touches.first?.view {
            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("OMINViewController")
            navigationController?.tr_pushViewController(vc, method: .OMIN(keyView: view), completion: {
                print("Push finish")
            })
        }
    }
}

需要 pop 时,只需要调用public func tr_popViewController(completion: (() -> Void)? = nil) -> UIViewController?

执行一个 Present 的转场

假设我们现在是从MainViewController Present 到ModalViewController

  • MainViewController必须服从ModalViewControllerDelegate,并添加var tr_transition: TRViewControllerTransitionDelegate?
  • 同时ModalViewController必须服从MainViewControllerDelegate,并添加weak var modalDelegate: ModalViewControllerDelegate?

为什么看起来会很麻烦,因为 dismiss 我们的ModalViewController应该是由MainViewController执行,我们需要modalDelegate。关于这个问题,我暂时没有在官方的文档中找到,但你可以阅读 Dismissing a Presented View Controller

示例代码如下:

/// MainViewController.swift
var tr_transition: TRViewControllerTransitionDelegate?

@IBAction func tr_presentVC(sender: UIButton) {
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ModalViewController") as! ModalViewController
        vc.modalDelegate = self
        let nav = UINavigationController(rootViewController: vc)
        tr_presentViewController(nav, method: .Twitter, completion: nil)
    }

/// ModalViewController.swift
weak var modalDelegate: ModalViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func pop(sender: AnyObject) {
        modalDelegate?.modalViewControllerDismiss(callbackData: ["data":"back"])
    }
Clone this wiki locally