问题描述
当将视图控制器推送到 UINavigationController
时,如何获得自定义转换(iOS7)?我尝试在 UINavigationController
中设置 TransitioningDelegate
,也在控制器上设置
方法
div>
@rounak有正确的想法,但有时它有助于准备代码,而无需从github下载。
这里是我采取的步骤:
-
使您的FromViewController.m符合
UINavigationControllerDelegate
。其他的示例代码告诉你符合UIViewControllerTransitioningDelegate
,但这只有当你 ToViewController。
@interface ViewController:UIViewController
-
在FromViewController中的委托回调方法中返回自定义过渡动画对象:
- (id< UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController: (UIViewController *)fromVC
toViewController:(UIViewController *)toVC {
TransitionAnimator * animator = [TransitionAnimator new];
animator.presenting =(operation == UINavigationControllerOperationPush);
return animator;
}
-
创建自定义animator类并粘贴这些示例方法: p>
- (NSTimeInterval)transitionDuration:(id< UIViewControllerContextTransitioning>)transitionContext {
return 0.5f;
}
- (void)animateTransition:(id< UIViewControllerContextTransitioning>)transitionContext {
//从上下文中获取from和to控制器
UIViewController * fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController * toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//设置我们的结束帧。如果我们要
,我们将修改它。CGRect endFrame = CGRectMake(80,280,160,100);
if(self.presenting){
fromViewController.view.userInteractionEnabled = NO;
[transitionContext.containerView addSubview:fromViewController.view];
[transitionContext.containerView addSubview:toViewController.view];
CGRect startFrame = endFrame;
startFrame.origin.x + = 320;
toViewController.view.frame = startFrame;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^ {
fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
toViewController.view.frame = endFrame;
} completion:^(BOOL finished){
[transitionContext completeTransition:YES];
}];
}
else {
toViewController.view.userInteractionEnabled = YES;
[transitionContext.containerView addSubview:toViewController.view];
[transitionContext.containerView addSubview:fromViewController.view];
endFrame.origin.x + = 320;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^ {
toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
fromViewController.view.frame = endFrame;
} completion:^(BOOL finished){
[transitionContext completeTransition:YES];
}];
}
}
本质上,动画师是做重的东西的对象。当然,你可以让你的UINavigationControllerDelegate是一个单独的对象,但这取决于你的架构师你的应用程序。
How can I get custom transitions (iOS7) when pushing a view controller onto UINavigationController
? I tried setting the TransitioningDelegate
both in the UINavigationController
and also on the controller I'm pushingThe methods never get called.
All examples I find use custom transitions when presenting modally.
@rounak has the right idea, but sometimes it helps to have code ready without having to download from github.
Here are the steps that I took:
Make your FromViewController.m conform to
UINavigationControllerDelegate
. Other sample code out there tells you to conform toUIViewControllerTransitioningDelegate
, but that's only if you're presenting the ToViewController.@interface ViewController : UIViewController
Return your custom transition animator object in the delegate callback method in FromViewController:
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { TransitionAnimator *animator = [TransitionAnimator new]; animator.presenting = (operation == UINavigationControllerOperationPush); return animator; }
Create your custom animator class and paste these sample methods:
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.5f; } - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext { // Grab the from and to view controllers from the context UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; // Set our ending frame. We'll modify this later if we have to CGRect endFrame = CGRectMake(80, 280, 160, 100); if (self.presenting) { fromViewController.view.userInteractionEnabled = NO; [transitionContext.containerView addSubview:fromViewController.view]; [transitionContext.containerView addSubview:toViewController.view]; CGRect startFrame = endFrame; startFrame.origin.x += 320; toViewController.view.frame = startFrame; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed; toViewController.view.frame = endFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } else { toViewController.view.userInteractionEnabled = YES; [transitionContext.containerView addSubview:toViewController.view]; [transitionContext.containerView addSubview:fromViewController.view]; endFrame.origin.x += 320; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic; fromViewController.view.frame = endFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } }
Essentially, the animator is the object doing the heavy lifting. Of course, you can make your UINavigationControllerDelegate be a separate object, but that depends on how your architect your app.
这篇关于如何使用UIViewControllerAnimatedTransitioning与UINavigationController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!