未调用自定义转换

时间:2017-07-27 07:11:20

标签: ios swift

在Swift中,我有一个自定义的过渡,我试图这样做。

import Foundation
import UIKit

class CustomPushAnimation: NSObjectUIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.2
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerVw = transitionContext.containerView
        let fromViewController = transitionContext.viewController(forKey: .from)
        let toViewController = transitionContext.viewController(forKey: .to)
        guard let fromVc = fromViewController, let toVc = toViewController else { return }
        let finalFrame = transitionContext.finalFrame(for: toVc)

        //For different animation you can play around this line by changing frame
        toVc.view.frame = finalFrame.offsetBy(dx: 0, dy: finalFrame.size.height/2)
        containerVw.addSubview(toVc.view)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            toVc.view.frame = finalFrame
            fromVc.view.alpha = 0.5
        }, completion: {(finished) in
            transitionContext.completeTransition(finished)
            fromVc.view.alpha = 1.0
        })
    }
}

然后在主视图控制器中我有一个名为settings的按钮,我已经完成了这个

lazy var customPushAnimation: CustomPushAnimation = {
    return CustomPushAnimation()
}()

func openViewController() {
    let vc = SettingsViewController()
    let navigationController = UINavigationController(rootViewController: vc)
    navigationController.transitioningDelegate = self.customPushAnimation
    present(navigationController, animated: true, completion: nil)
}

然后我也有这个扩展名:

extension ViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return customPushAnimation
    }
}

然后当单击该按钮时,将调用openViewController函数,并且将使用我拥有的自定义动画显示我想要打开的视图控制器。相反,我只是继续获得在导航控制器中使用show segue时的标准滑动动画。

0 个答案:

没有答案