简体   繁体   English

如何在UINavigationController中使用UIViewControllerAnimatedTransitioning?

[英]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: 以下是我采取的步骤:

  1. 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

  2. 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; } 
  3. 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.

相关问题 UIViewControllerAnimatedTransitioning UINavigationController push和pop - UIViewControllerAnimatedTransitioning UINavigationController push and pop 如何提供自定义UIViewControllerAnimatedTransitioning并将默认UIViewControllerInteractiveTransitioning用于导航控制器 - How to provide custom UIViewControllerAnimatedTransitioning and use default UIViewControllerInteractiveTransitioning for a navigation controller 如何在没有情节提要/展开Segues的Swift中使用UIViewControllerAnimatedTransitioning? - How to use UIViewControllerAnimatedTransitioning in Swift without Storyboards / Unwindng Segues? 如何在UINavigationController中使用UIImagePickerController - How to use a UIImagePickerController with UINavigationController 如何在 SwiftUI 中使用 UINavigationController - how to use UINavigationController in SwiftUI 如何将 UIDocumentPickerViewController 与 UINavigationController 一起使用? - How to use UIDocumentPickerViewController with UINavigationController? 如何使用uinavigationcontroller - How to use uinavigationcontroller 如何正确使用UINavigationController - How to properly use a UINavigationController 如何在 UINavigationController 中使用协议和委托 - How to use Protocols and Delegates with UINavigationController 如何在AppDelegate之外使用UINavigationController? - How to use UINavigationController outside the AppDelegate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM