我刚刚注意到:
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
没有被称为id我推送UIViewController
:
navigationController?.pushViewController(nextVC, animated: true)
否则,如果我出示UIViewController
:
navigationController?.presentViewController(nextVC, animated: true, completion: nil)
我的动画有效,因为animateTransition被称为
修改
它的使用方式如下:
let transitionManager = TransitionManager()
nextVC.transitioningDelegate = transitionManager
navigationController?.pushViewController(nextVC, animated: true)
完整的动画类是:
class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
private var presenting = true
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let container = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
let offScreenRight = CGAffineTransformMakeTranslation(container!.frame.width, 0)
let offScreenLeft = CGAffineTransformMakeTranslation(-container!.frame.width, 0)
toView.transform = offScreenRight
container!.addSubview(toView)
container!.addSubview(fromView)
let duration = self.transitionDuration(transitionContext)
UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: [], animations: {
fromView.transform = offScreenLeft
toView.transform = CGAffineTransformIdentity
}) { (finished) in
transitionContext.completeTransition(true)
}
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 2
}
// MARK: UIViewControllerTransitioningDelegate protocol methods
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = true
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = false
return self
}
}
答案 0 :(得分:3)
问题是你在说:
nextVC.transitioningDelegate = transitionManager
这就是您配置当前自定义转换的方式。但不如何配置推送自定义转换。
要配置推送自定义转换,请为导航控制器指定delegate
(而非transitioningDelegate
),然后在委托中执行:
func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController) ->
UIViewControllerAnimatedTransitioning? {
答案 1 :(得分:0)
我不知道CGAffineTransformMakeTranslation,但我使用CATransitions做动画。在下面的代码中设置所需的动画(我假设你想从左边缘滑动):
let transition = CATransition()
transition.duration = 1.0;
transition.type = kCATransitionPush; //choose your animation
transition.subtype = kCATransitionFromLeft
navigationController?.view.layer.add(transition, forKey: nil)
navigationController?.pushViewController(nextVC, animated: false)