快速实现视图控制器之间的平滑自定义转换

发布于 2025-01-11 07:37:54 字数 678 浏览 1 评论 0原文

输入图片这里的描述任何人都可以帮助我修复在视图控制器上使用自定义转换到视图控制器时这个恼人的跳转/故障错误。我无法使用推送,因为我想显示顶部导航栏但隐藏底部选项卡栏,因此我必须使用自定义转换。我想实现平滑的过渡,就像从导航控制器上的视图控制器转到视图控制器一样。我已附上我的代码。

 func presentDetail(_ viewControllerToPresent: UIViewController) {
        let transition = CATransition()
        transition.duration = 0.25
        transition.type = CATransitionType.push
        transition.subtype = CATransitionSubtype.fromRight
        self.view.window!.layer.add(transition, forKey: kCATransition)
        present(viewControllerToPresent, animated: false)
    }

enter image description herecan anyone help me fix this annoying jump/glitch bug when using a custom transition on view controller to view controller. I can't use push because I want to display the top navigation bar but hide the bottom tab bar so I have had to use a custom transition. I want to acheive a smooth transition like when going from view controller to view controller on a navigation controller. I have attached my code.

 func presentDetail(_ viewControllerToPresent: UIViewController) {
        let transition = CATransition()
        transition.duration = 0.25
        transition.type = CATransitionType.push
        transition.subtype = CATransitionSubtype.fromRight
        self.view.window!.layer.add(transition, forKey: kCATransition)
        present(viewControllerToPresent, animated: false)
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2025-01-18 07:37:54

这与给出的评论没有太大不同,但只是一些不容易添加到评论中的解释。

我不认为您看到的是一个小故障,而是使用 CATransition 自动应用的 fade - 您可以看到它变慢了。

自定义推送Transition UIViewController

您可以使用 CABasicAnimation 或自定义

href="https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/CustomizingtheTransitionAnimations.html" rel="nofollow noreferrer">UIViewControllerTransitions CATransition swift iOS 但这些将是我要做的 不确定为什么 hidesBottomBarWhenPushed 不适合您。

我有以下视图层次结构:

UITabBarController
   - UINavivationController (Tab 1)
     - UIViewController
   - UIViewController2 (Tab 2)

我现在这样做:

let dtvc = DetailTransitionVC()
dtvc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(dtvc, animated: true)

这似乎给了我你想要的:

UINavigationController 推送不带标签栏删除 iOS Swift

This is not so different from the comments given, however just some explanations that were not easy to add in comments.

I don't think what you see is a glitch, but a fade that is automatically applied with CATransition - you can see it slowed down.

Custom Push Transition UIViewController

You could go the CABasicAnimation or customizing UIViewControllerTransitions CATransition swift iOS but these will be a lot more work

I am not sure why hidesBottomBarWhenPushed does not work for you.

I have the following view hierarchy:

UITabBarController
   - UINavivationController (Tab 1)
     - UIViewController
   - UIViewController2 (Tab 2)

I now do this:

let dtvc = DetailTransitionVC()
dtvc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(dtvc, animated: true)

And this seems to give me what you want:

UINavigationController push without tab bar remove iOS Swift

橘亓 2025-01-18 07:37:54

我也刚刚经历过这种行为。罪魁祸首在于我的转变方式。我正在推动使用此功能的 VC,因为我也想支持淡入淡出过渡。以前,我使用 .push 作为 AnimationType 来应用过渡。这导致了您在 GIF 中看到的奇怪效果。现在,我将动画设置为可选,只有在未定义的情况下才会 applyTransition。否则,它将使用默认的内置过渡,这是平滑的。

open func push(vc: UIViewController, animation: AnimationType? = nil) {
    guard let viewController = self.viewController else { return }
    
    if let animation = animation {
        applyTransition(to: viewController.navigationController?.view, animation: animation)
    }
    
    viewController.navigationController?.pushViewController(vc, animated: animation == nil)
}

这是 applyTransition:

private func applyTransition(to view: UIView?, animation: AnimationType) {
    guard let view = view else { return }
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    switch animation {
        case .push:
            transition.type = CATransitionType.push
            transition.subtype = CATransitionSubtype.fromRight
        case .fade:
            transition.type = CATransitionType.fade
    }
    view.layer.add(transition, forKey: nil)
}

I was just experiencing this behavior as well. The culprit lied in how I was transitioning. I was pushing a VC using this function because I also wanted to support fade transitions. Previously, I was using .push as the AnimationType to apply the transition. This was causing that weird effect you see in the GIF. Instead now, I made animation an optional which only will applyTransition if it is not undefined. Otherwise, it will use the default built in transition which is smooth.

open func push(vc: UIViewController, animation: AnimationType? = nil) {
    guard let viewController = self.viewController else { return }
    
    if let animation = animation {
        applyTransition(to: viewController.navigationController?.view, animation: animation)
    }
    
    viewController.navigationController?.pushViewController(vc, animated: animation == nil)
}

Here is applyTransition:

private func applyTransition(to view: UIView?, animation: AnimationType) {
    guard let view = view else { return }
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    switch animation {
        case .push:
            transition.type = CATransitionType.push
            transition.subtype = CATransitionSubtype.fromRight
        case .fade:
            transition.type = CATransitionType.fade
    }
    view.layer.add(transition, forKey: nil)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文