简体   繁体   English

如何推送两个视图控制器但仅为第二个视图控制器设置动画过渡?

[英]How to push two view controllers but animate transition only for the second one?

I have three controllers (FirstVC, SecondVC, ThirdVC) inside storyboad, and navigation is sequential: a user can navigate from FirstVC to SecondVC, and then to ThirdVC.我在 storyboard 中有三个控制器(FirstVC、SecondVC、ThirdVC),导航是顺序的:用户可以从 FirstVC 导航到 SecondVC,然后导航到 ThirdVC。 Now, I need to make some button that will open ThirdVC from FirstVC but will also put SecondVC on navigation stack, so when a user will press back from ThirdVC he will be returned to SecondVC.现在,我需要制作一些按钮,可以从 FirstVC 打开 ThirdVC,但也会将 SecondVC 放在导航堆栈上,因此当用户从 ThirdVC 按下时,他将返回到 SecondVC。 So, I don't need animation from FirstVC to SecondVC, just need to push SecondVC on navigation controller stack and then animate only transition to ThirdVC.所以,我不需要从 FirstVC 到 SecondVC 的动画,只需要将 SecondVC 推送到导航控制器堆栈上,然后动画只转换到 ThirdVC。

I was unable to find how disable animation for performSegueWithIdentifier, so I'm thinking I should instantiate SecondVC from storyboard manually, put it on navigation stack, and then perform performSegueWithIdentifier for ThirdVC.我无法找到如何为 performSegueWithIdentifier 禁用动画,所以我想我应该手动从故事板实例化 SecondVC,将它放在导航堆栈上,然后为 ThirdVC 执行 performSegueWithIdentifier。 Any ideas how to do that?任何想法如何做到这一点?

The solution you're looking for if you're in the firstVC:如果您在 firstVC 中,您正在寻找的解决方案:

NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
[controllers addObject:secondVc];
[controllers addObject:thirdVC];
[self.navigationController setViewControllers:controllers animated:YES];

This will animate in the thirdVC without the secondVc becoming visible in the process.这将在第三个VC 中进行动画处理,而第二个VC 在此过程中不会变得可见。 When the user press the back button, they will return to the secondVc当用户按下后退按钮时,他们将返回到第二个Vc

To preserve the standard animation for pushing a view controller, in Swift :要保留推送视图控制器的标准动画,在 Swift 中

let pushVC = UIViewController()
let backVC = UIViewController()

if let navigationController = navigationController {

  navigationController.pushViewController(pushVC, animated: true)

  let stackCount = navigationController.viewControllers.count
  let addIndex = stackCount - 1
  navigationController.viewControllers.insert(backVC, atIndex: addIndex)

}

This displays pushVC normally and inserts backVC into the navigation stack, preserving both the animation and the history for the UINavigationController .这会正常显示pushVC并将backVC插入导航堆栈,同时保留UINavigationController的动画和历史记录。

You can use setViewControllers , but you'll lose the standard push animation.您可以使用setViewControllers ,但您将失去标准的推送动画。

Swift Version From KimAMartinsen Solution来自 KimAMartinsen 解决方案的 Swift 版本

guard var controllers = self.navigationController?.viewControllers else {
   return
}

guard let firstVC = self.storyboard?.instantiateViewController(withIdentifier: "Tests") as? firstViewController else { 
   return
}

guard let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "Dashboard") as? SecondViewController else {
    return
}

controllers.append(firstVC)
controllers.append(secondVC)
self.navigationController?.setViewControllers(controllers, animated: true)
extension UINavigationController {
    open func pushViewControllers(_ inViewControllers: [UIViewController], animated: Bool) {
        var stack = self.viewControllers
        stack.append(contentsOf: inViewControllers)
        self.setViewControllers(stack, animated: animated)
    }
}

Hmm perhaps just write a custom segue that doesn't do any animations an make sure the segue in the storyboard references your segue class.嗯,也许只是编写一个不做任何动画的自定义 segue,并确保故事板中的 segue 引用您的 segue 类。

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html

Link above to how to create it, docs IMO are fairly self explanatory.上面链接到如何创建它,IMO 文档是相当不言自明的。 You can then customise how you segues work and appear.然后,您可以自定义 segue 的工作和显示方式。

Other options include the new protocols introduced in iOS7 assuming you don't want to support older devices.其他选项包括 iOS7 中引入的新协议,假设您不想支持旧设备。

Watch the Apple Tech Talks 2014 video "Architecting Modern Apps part 1" they demo it on there.观看 Apple Tech Talks 2014 视频“Architecting Modern Apps part 1”,他们在那里进行了演示。

https://developer.apple.com/tech-talks/videos/ https://developer.apple.com/tech-talks/videos/

There are many solutions to your question hopefully one of the above is helpful, let me know if it's not and I'll propose another.您的问题有很多解决方案,希望上述方法之一对您有所帮助,如果不是,请告诉我,我会提出另一个方案。

UPDATE:更新:

Another option would be to use a tav view controller perhaps if this fits in with your needs as you could add a navigation controller to one of the tabs to achieve this or swap tabs as needed.另一种选择是使用 tav 视图控制器,如果这符合您的需求,因为您可以将导航控制器添加到其中一个选项卡以实现此目的或根据需要交换选项卡。

I suggest pushing your view controllers manually.我建议手动推送您的视图控制器。 The first without animation:第一个没有动画:

[self.navigationController pushViewController:SecondVC animated:NO];
[self.navigationController pushViewController:ThirdVC animated:YES];

I use the following snippet to push multiple View Controllers:我使用以下代码段来推送多个视图控制器:

extension UINavigationController {
    func push(_ viewControllers: [UIViewController]) {
        setViewControllers(self.viewControllers + viewControllers, animated: true)
    }

    // Also had this in here, left it in as a bonus :)
    func popViewControllers(_ count: Int) {
        guard viewControllers.count > count else { return }
        popToViewController(viewControllers[viewControllers.count - count - 1], animated: true)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为两个模态视图控制器之间的过渡设置动画? - How can I animate the transition between two modal view controllers? 如何在不使用两个视图控制器的情况下为两个视图之间的过渡设置动画? - How to animate transition between two views without using two view controllers? 如何在不看到第一个视图控制器的情况下模态推送两个视图控制器 - How to push two view controllers modally without seeing the first one 如何在 SWIFT 3 中使用按钮为 3 个或更多视图控制器之间的过渡设置动画? - How to animate a transition between 3 or more view controllers with buttons in SWIFT 3? iOS:容器视图 - 更改子视图控制器时的动画推送转换 - iOS: Container view - animate push transition when changing child view controllers 动态化视图控制器从一个到另一个的过渡,而无需导航控制器堆栈 - Animate transition of view controllers from one to another without navigation controller stack 使用两个视图控制器,第二个将不显示视图 - Using two view controllers, second one won't display views 如何从多个视图控制器推送到一个视图控制器对象 - How to push to one view controller object from multiple view controllers 如何用一个按钮连接两个视图控制器? - How to connect two view controllers with one button? 两个视图控制器,一个视图? - Two view controllers, one view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM