[英]How to use UIViewControllerAnimatedTransitioning with UINavigationController?
How can I get custom transitions (iOS7) when pushing a view controller onto UINavigationController
? 将视图控制器推送到
UINavigationController
时,如何获得自定义转换(iOS7)? I tried setting the TransitioningDelegate
both in the UINavigationController
and also on the controller I'm pushing The methods never get called. 我尝试在
UINavigationController
和我正在推动的控制器上设置TransitioningDelegate
。方法永远不会被调用。
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. @rounak有正确的想法,但有时无需从github下载就可以准备好代码。
Here are the steps that I took: 以下是我采取的步骤:
Make your FromViewController.m conform to UINavigationControllerDelegate
. 使FromViewController.m符合
UINavigationControllerDelegate
。 Other sample code out there tells you to conform to UIViewControllerTransitioningDelegate
, but that's only if you're presenting the ToViewController. 其他示例代码告诉您符合
UIViewControllerTransitioningDelegate
,但这只是在您呈现 ToViewController时。
@interface ViewController : UIViewController @interface ViewController:UIViewController
Return your custom transition animator object in the delegate callback method in FromViewController: 在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.
当然,您可以将UINavigationControllerDelegate设置为单独的对象,但这取决于您的应用程序的架构师。
objc.io's post on view controller transitions are specifically for pushing and popping view controllers. objc.io在视图控制器转换上的帖子专门用于推送和弹出视图控制器。 http://objc.io/issue-5/view-controller-transitions.html
http://objc.io/issue-5/view-controller-transitions.html
I've done this animation ( http://i.imgur.com/1qEyMu3.gif ) solely based on the objc.io post. 我完全根据objc.io帖子完成了这个动画( http://i.imgur.com/1qEyMu3.gif )。
In short you have to have a class(es) implementing UINavigationControllerDelegate
, and UIViewControllerAnimatedTransitioning
with the required methods for returning the correct animator, and performing the animations. 简而言之,你必须有一个实现
UINavigationControllerDelegate
的类,以及UIViewControllerAnimatedTransitioning
具有返回正确的动画师和执行动画所需的方法。
You can look at my demo project which demonstrates using custom transitions in UINavigationController
. 您可以查看我的演示项目,演示如何在
UINavigationController
使用自定义过渡。 Look at https://github.com/Vaberer/BlurTransition . 请查看https://github.com/Vaberer/BlurTransition 。
EDIT: Just realised this might not answer your question. 编辑:刚刚意识到这可能无法回答你的问题。 But it is an alternative.
但它是另一种选择。
If you're using a storyboard you can do a custom transition by creating a custom segue. 如果您正在使用故事板,则可以通过创建自定义segue来执行自定义转换。 In the attributes inspector change the segue class name to your custom transition class eg
MySegue
. 在属性检查器中,将segue类名称更改为自定义转换类,例如
MySegue
。 Then create the MySegue
class and implement the -(void)perform
method to perform your transition. 然后创建
MySegue
类并实现-(void)perform
方法来执行转换。
- (void) perform{
UIViewController *source = self.sourceViewController;
UIViewController *destination = self.destinationViewController;
[UIView transitionFromView:source.view
toView:destination.view
duration:0.50f
options:UIViewAnimationOptionTransitionFlipFromTop
completion:nil];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.