【问题标题】:Custom transition between UIViewControllers does not workUIViewControllers 之间的自定义转换不起作用
【发布时间】:2016-04-20 22:34:07
【问题描述】:

我有管理自定义转换的对象:

    @interface NavigationManager : NSObject <UIViewControllerAnimatedTransitioning>

    -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;
    -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;

    @property (nonatomic, assign) NSTimeInterval presentationDuration;
    @property (nonatomic, assign) NSTimeInterval dismissalDuration;
    @property (nonatomic, assign) BOOL isPresenting;


    @end

    @interface NavigationManager()

    @property (nonatomic, strong) id<UIViewControllerContextTransitioning> transitionContext;

    @end

    @implementation NavigationManager

    @synthesize presentationDuration, dismissalDuration, isPresenting;

    -(id)init{
        self = [super init];

        if(self){

            self.presentationDuration = 1.0;
            self.dismissalDuration = 0.5;
        }

        return self;
    }

    -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{

        return self.isPresenting ? self.presentationDuration : self.dismissalDuration;
    }

    -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{


        self.transitionContext = transitionContext;
        if(self.isPresenting){
            [self executePresentation:transitionContext];
        }
        else{

            [self executeDismiss:transitionContext];
        }

    }

    ...


    @end

在应该处理实现转换的类中:

@interface ViewController : UIViewController <UIViewControllerTransitioningDelegate>

@property (nonatomic, strong) NavigationManager navigationManager;

..

@end

@implementation

...

//这里我正在导航到新的 UIViewController

 - (void)navigateToNewScreen
{
    DetailedNewsController *secVC = [[DetailedNewsController alloc] init];
    secVC.fullDescription = fullDescription;
    secVC.headerImage = a.imageView.image;
    self.transitioningDelegate = self;
    [self.navigationController pushViewController:secVC animated:YES];
}

 - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
    self.animationController.isPresenting = YES;
    return self.animationController;
}

 - (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.animationController.isPresenting = NO;
    return self.animationManager;
}

@end

执行默认的过渡动画。导航后也不显示导航控制器栏。

更新:

我在 AppDelegate 中创建 UINavigationController 的方式存在问题:

firstVC = [[ViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:firstVC];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];

【问题讨论】:

  • 我从来没有为推送做过自定义过渡,但不应该是secVC.transitioningDelegate = self;吗?
  • 改为:secVC.transitioningDelegate = self; secVC.modalPresentationStyle = UIModalPresentationCustom; [self presentViewController:secVC 动画:YES 完成:nil];它有效。但是导航栏是隐藏的
  • 检查我的答案,让我知道它是否有效

标签: ios iphone uiviewanimationtransition


【解决方案1】:

我会用你提供的新信息来回答。我之所以把它作为答案,是因为我只是不想给你一些复制粘贴代码,而是试图在它背后给出一个简短的解释。如果我理解正确,您现在想要呈现一个带有自定义转换的 ViewController。

因此,您可以通过将代码更改为以下代码来实现自定义转换:

secVC.transitioningDelegate = self; 
secVC.modalPresentationStyle = UIModalPresentationCustom; 
[self presentViewController:secVC animated:YES completion:nil];

自从我们启动并运行它后,目前缺少的是您要显示的 ViewController 的 NavigationBar。由于您正在呈现一个 ViewController,它不会包含在您正在呈现的 ViewController 所在的现有 NavigationController 堆栈中。

因此,您需要将要呈现的 VC 包装在 UINavigationController 中。

[self presentViewController:[[UINavigationController alloc] initWithRootViewController:secVC] animated:YES completion:nil];

【讨论】:

  • 谢谢,我已经试着换行了。但在这种情况下,自定义动画没有出现,导航栏没有后退按钮。我想 viewController 应该作为孩子而不是根添加到堆栈中
  • 我以为你想送礼物?在这种情况下,后退按钮不会自动添加到 NavigationBar,因为它没有被添加到任何导航控制器堆栈中
  • 我也想拥有返回根控制器的能力
  • 好的,很抱歉我的误解。所以当你改成secVC.transitioningDelegate = self;并调用[self.navigationController pushViewController:secVC animated:YES];时,你现在遇到的问题是secVC的NavigationBar被隐藏了?
  • 我使用了 presetViewController:动画似乎做得对,但无法关闭模态视图或返回根视图。因为 pushViewController 启动默认导航动画
猜你喜欢
  • 2013-07-20
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多