【问题标题】:transition between ViewControllers not animating correct VCViewControllers 之间的转换没有为正确的 VC 设置动画
【发布时间】:2021-01-28 23:14:23
【问题描述】:

所以我正在尝试为我的 UIViewController 之间的牵引设置动画。我希望新的 VC 从按钮所在的同一侧推入。我已经设法从右侧推入一个 VC,但是当我尝试从左侧推入时,它会先推入同一个 VC,然后弹出新的 VC。

这是我的代码:

    switch sender.view {
    case settingsButton:
        let settingScreen = SettingsVC()
        settingScreen.modalPresentationStyle = .fullScreen
        present(settingScreen, animated: false, completion: nil)
        let transition      = CATransition()
        transition.duration = 0.7
        transition.type     = .moveIn
        transition.subtype  = .fromLeft
        transition.timingFunction = CAMediaTimingFunction(name: .default)
        view.window!.layer.add(transition, forKey: kCATransition)
    case calendarButton:
        let calendarScreen  = CalendarVC()
        calendarScreen.modalPresentationStyle = .fullScreen
        self.present(calendarScreen, animated: false, completion: nil)
        let transition      = CATransition()
        transition.duration = 0.5
        transition.type     = .moveIn
        transition.subtype  = .fromRight
        transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        view.window!.layer.add(transition, forKey: kCATransition)
    default:
        break
    }

【问题讨论】:

  • 我需要导航控制器来执行此操作吗?

标签: ios swift animation uiviewcontroller


【解决方案1】:

您不应该使用 CATransition 来实现这一点。有了它,您可以在单个 UIViewController 中为视图转换设置动画。 UIViewController 转换有点不同。

Apple 有自己的 UIViewController 转换 API,但非常麻烦。

在我看来,最好的解决方案是使用来自 Github 的 Hero 库。 它没有陡峭的学习曲线,这种简单的动画很容易实现。

【讨论】:

  • 好的,我正在尝试英雄。我需要为此使用导航控制器吗?
  • 你没有。您可以在模态显示 VC 时指定动画
  • 所以我应该可以直接进入每个 VC 并指定我想要的 hero 动画?
  • 是的,试试看。
猜你喜欢
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多