使用默认的UINavigationInteractiveTransition交互式动画UIView

时间:2016-09-12 18:48:23

标签: ios swift uinavigationcontroller uikit uiviewanimationtransition

在我的应用中,我想在默认UIView下为UINavigationInteractiveTransition制作动画。当我不使用互动PanGesture时,它就像一个魅力。

有没有办法通过UIViewControllerTransitionCoordinator

对交互式动画的当前状态做出反应

有关实施细节,请参阅我创建的Playground:

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground

class CustomNavigationController: UINavigationController {

    var customView: UIView!

    override func viewDidLoad() {
        customView = UIView(frame: CGRect(x: 0, y: 400, width: 300, height: 300))
        customView.backgroundColor = UIColor.redColor()

        view.addSubview(customView)
    }
}

class CustomViewController: UIViewController {

    var dismissButton: UIButton!

    dynamic func dismissCustomViewController() {
        navigationController?.popViewControllerAnimated(true)
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        transitionCoordinator()?.animateAlongsideTransition({ (context) in
            (self.navigationController as? CustomNavigationController)?.customView.backgroundColor = UIColor.redColor()
            }, completion: { (context) in
                // not implemented
        })
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        transitionCoordinator()?.animateAlongsideTransition({ (context) in
            (self.navigationController as? CustomNavigationController)?.customView.backgroundColor = UIColor.yellowColor()
            }, completion: { (context) in
                // not implemented
        })
    }

    override func viewDidLoad() {

        title = "Second Controller"
        view.backgroundColor = UIColor.purpleColor()

        dismissButton = addButton(withTitle: "Dismiss Controller", andSelector: #selector(dismissCustomViewController))
    }
}

class FirstViewController: UIViewController {

    var pushButton: UIButton!

    dynamic func pushCustomViewController() {
        let customViewControlelr = CustomViewController()
        navigationController?.pushViewController(customViewControlelr, animated: true)
    }

    override func viewDidLoad() {

        title = "FirstController"

        view.backgroundColor = UIColor.whiteColor()

        pushButton = addButton(withTitle: "Push Controller", andSelector: #selector(pushCustomViewController))
    }
}

extension UIViewController {

    func addButton(withTitle title: String, andSelector selector: Selector) -> UIButton {

        let button = UIButton(type: .Custom)
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.addTarget(self, action: selector, forControlEvents: .TouchUpInside)

        view.addSubview(button)

        let views = ["button" : button]

        let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[button]-20-|", options: [], metrics: nil, views: views)
        let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[button(60)]", options: [], metrics: nil, views: views)
        view.addConstraints(constraintsH)
        view.addConstraints(constraintsV)

        return button
    }
}

let firstViewController = FirstViewController()
let navController = CustomNavigationController(rootViewController: firstViewController)


XCPlaygroundPage.currentPage.liveView = navController.view

0 个答案:

没有答案