【问题标题】:Is there any way to making transition without storyboard?没有故事板有没有办法进行过渡?
【发布时间】:2019-12-24 11:38:12
【问题描述】:

我尝试制作过渡动画以在两个 viewController 之间进行切换。有什么方法可以在不使用情节提要的情况下做到这一点?

这段代码用于调用新的viewController(我没有使用storyboard):

 @objc func handleAccount() {
    let navController = UINavigationController(rootViewController: userButtonLauncher())
    navController.transitioningDelegate = self
    navController.modalPresentationStyle = .custom
    self.present(navController, animated: true, completion: nil)
}

这个转换代码:

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.transitionMode = .present
    transition.startingPoint = CGPoint(x: 0, y: 0)
    return transition
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.transitionMode = .pop
    transition.startingPoint = CGPoint(x: 0, y: 0)
    return transition
}

【问题讨论】:

  • 尝试搜索push viewController programmatically
  • 嗨,Lu,我已经尝试过推送 viewController,但我认为转换页面覆盖了 viewController。当我写 'let navController = UINavigationController(rootViewController: userButtonLauncher()) navController.transitioningDelegate = self navController.modalPresentationStyle = .custom self.present(navController, animated: true, completion: nil)' 时。转换页面下的 UserButtonLauncher。我只看到空白页。

标签: ios swift iphone xcode swift3


【解决方案1】:

当然可以!这取决于您想做什么,因此如果您需要在代码上使用默认动画而不使用情节提要,您可以: 实例化你的 ViewController,设置你​​想要的动画并打开 ViewController

let vc = self.storyboard?.instantiateViewController(withIdentifier: "vc_id") vc.modalTransitionStyle = .flipHorizontal self.present(vc, animated: true, completion: nil)

否则,您可以创建自定义过渡,请参阅以下简单指南: https://www.raywenderlich.com/322-custom-uiviewcontroller-transitions-getting-started

【讨论】:

  • 感谢马尔科克的回答。我是 swift 新手,我尝试了很多方法来做到这一点,但我找不到任何方法。如果我问得更清楚,我会写 CircularTransition.swift 但我没有使用 segue,所以我用 navigationController 代码调用第二个 viewController 但我认为我的第二个控制器覆盖了我的第二个控制器,transitionDelegate 和 modalTransitionStyle 代码。不知道我解释的对不对?
  • 没问题!我建议您使用项目的一些代码来更新您的问题,这样更容易为您提供帮助。无论如何,请尝试评论 transitionDelegate 行,看看它是否仍然被覆盖
  • 我试图编辑我的问题。我希望我能更清楚地解释我的问题。感谢您的帮助!
  • 谢谢!你从哪里打电话给 handleAccount ?从过渡班?
  • 实际上我不能在我的转换类中调用handleAccount,因为我将圆圈创建为UIView()。我找不到在过渡类中调用该函数的方法。有什么方法可以打电话吗?
【解决方案2】:

我解决了我的问题。我只是在我的 homecontroller 中添加了调用动画转换的代码:

extension HomeController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return faceanim()

}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return faceanim()

}

}

而按钮动作是:

    @objc func handleAccount() {

    let userConVC = UINavigationController(rootViewController: userButtonLauncher())
    userConVC.transitioningDelegate = self
    navigationController?.isNavigationBarHidden = false
    self.present(userConVC, animated: true, completion: nil)

}

【讨论】:

  • 请将此标记为已解决,以便其他人可以快速找到解决方案。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多