Ios 如何将UIViewControllerAnimatedTransitioning与UINavigationController一起使用?

Ios 如何将UIViewControllerAnimatedTransitioning与UINavigationController一起使用?,ios,cocoa-touch,ios7,xamarin.ios,Ios,Cocoa Touch,Ios7,Xamarin.ios,在将视图控制器推送到UINavigationController上时,如何获得自定义转换(iOS7)?我尝试在UINavigationController和我正在推的控制器上设置transitiondelegate 这些方法永远不会被调用 我发现的所有示例在以模式呈现时都使用自定义转换。编辑:刚刚意识到这可能无法回答您的问题。但这是另一种选择 如果您使用的是故事板,您可以通过创建自定义序列来进行自定义转换。 在属性检查器中,将segue类名称更改为自定义转换类,例如MySegue。然后创建MyS

在将视图控制器推送到
UINavigationController
上时,如何获得自定义转换(iOS7)?我尝试在
UINavigationController
和我正在推的控制器上设置
transitiondelegate
这些方法永远不会被调用


我发现的所有示例在以模式呈现时都使用自定义转换。

编辑:刚刚意识到这可能无法回答您的问题。但这是另一种选择

如果您使用的是故事板,您可以通过创建自定义序列来进行自定义转换。 在属性检查器中,将segue类名称更改为自定义转换类,例如
MySegue
。然后创建
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];
}

io关于视图控制器转换的文章专门用于推送和弹出视图控制器

我只根据objc.io帖子制作了这个动画()


简言之,您必须有一个类来实现
UINavigationControllerDelegate
,以及
uiviewcontrolleranimatedtransition
,其中包含返回正确的动画师和执行动画所需的方法。

@rounak的想法是正确的,但有时,让代码准备就绪而不必从github下载会有所帮助

以下是我采取的步骤:

  • 使FromViewController.m符合
    UINavigationControllerDelegate
    。其他示例代码告诉您要遵守
    UIViewControllerTransitioningDelegate
    ,但这仅限于您演示ToViewController时

    @界面ViewController:UIViewController

  • 在FromViewController中的委托回调方法中返回自定义过渡animator对象:

    - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC {
        TransitionAnimator *animator = [TransitionAnimator new];
        animator.presenting = (operation == UINavigationControllerOperationPush);
        return animator;
    }
    
    -(id)导航控制器:(UINavigationController*)导航控制器
    AnimationControllerOperation:(UINavigationControllerOperation)操作
    fromViewController:(UIViewController*)fromVC
    toViewController:(UIViewController*)toVC{
    TransitionAnimator*animator=[TransitionAnimator new];
    animator.presenting=(操作==UINavigationControllerOperationPush);
    返回动画师;
    }
    
  • 创建自定义animator类并粘贴以下示例方法:

    - (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];
        }];
    }
    }
    
    -(NSTimeInterval)transitionDuration:(id)transitionContext{
    返回0.5f;
    }
    -(void)animateTransfion:(id)transitionContext{
    //从上下文中获取“从”和“到”视图控制器
    UIViewController*fromViewController=[transitionContext ViewControllerWorky:UITransitionContextFromViewControllerKey];
    UIViewController*toViewController=[transitionContext ViewControllerWorky:UITransitionContextToViewControllerKey];
    //设置结束帧。如果有必要,我们将稍后修改此帧
    CGRect endFrame=CGRectMake(80280160100);
    如果(自我介绍){
    fromViewController.view.userInteractionEnabled=否;
    [transitionContext.containerView添加子视图:fromViewController.view];
    [transitionContext.containerView addSubview:toViewController.view];
    CGRect startFrame=结束帧;
    startFrame.origin.x+=320;
    toViewController.view.frame=startFrame;
    [UIView animateWithDuration:[self-transitionDuration:transitionContext]动画:^{
    fromViewController.view.tintAdjustmentMode=UIViewTintAdjustmentMode变暗;
    toViewController.view.frame=结束帧;
    }完成:^(布尔完成){
    [transitionContext completeTransition:是];
    }];
    }
    否则{
    toViewController.view.userInteractionEnabled=是;
    [transitionContext.containerView addSubview:toViewController.view];
    [transitionContext.containerView添加子视图:fromViewController.view];
    endFrame.origin.x+=320;
    [UIView animateWithDuration:[self-transitionDuration:transitionContext]动画:^{
    toViewController.view.tintAdjustmentMode=UIViewTintAdjustmentModeAutomatic;
    fromViewController.view.frame=结束帧;
    }完成:^(布尔完成){
    [transitionContext completeTransition:是];
    }];
    }
    }
    

  • 从本质上说,动画师是做重物的对象。当然,您可以将UINavigationControllerDelegate设置为一个单独的对象,但这取决于您如何构建应用程序。

    您可以查看我的演示项目,该项目演示如何在
    UINavigationController
    中使用自定义转换。查看。

    请在回答您的问题时将答案标记为正确。您是否可以共享动画代码,我正在处理与图中所示类似的事情。我在推送和弹出(0.01,0.01)过程中给出了CGAffineTransform(translationX:1.02,y:1.02)似乎工作正常,我也想知道你的价值是什么:)我猜
    TransitionAnimator
    是UIViewControlleranimated转换类。我猜他把属性
    presenting
    添加到了它上面。