推送时未调用 animateTransition
animateTransition not called on push
我刚刚注意到:
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是:
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
}
}
我不知道 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)
问题是你在说:
nextVC.transitioningDelegate = transitionManager
这就是您配置 present 自定义转换的方式。但这 不是 如何配置 push 自定义转换。
要配置推送自定义转换,给导航控制器一个delegate
(不是transitioningDelegate
),并在委托中实现:
func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController) ->
UIViewControllerAnimatedTransitioning? {
我刚刚注意到:
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是:
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
}
}
我不知道 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)
问题是你在说:
nextVC.transitioningDelegate = transitionManager
这就是您配置 present 自定义转换的方式。但这 不是 如何配置 push 自定义转换。
要配置推送自定义转换,给导航控制器一个delegate
(不是transitioningDelegate
),并在委托中实现:
func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController) ->
UIViewControllerAnimatedTransitioning? {