UIPanGestureRecognizer弹出UIViewController [英] UIPanGestureRecognizer to pop UIViewController

查看:62
本文介绍了UIPanGestureRecognizer弹出UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否真的可以在推送的UIViewController上使用UIPanGestureRecognizer来实现类似的行为,例如在Telegram Messenger聊天视图(以及许多其他流行的Apps)中,您可以在其中从屏幕上的任意位置向右滑动即可返回菜单(或其他任何最初按下我们要查看的View Controller的View Controller).
我尝试了以下代码:

I'm wondering if it is actually possible to use a UIPanGestureRecognizer on a pushed UIViewController to achieve a similar behaviour like in the Telegram messenger chat view (and a lot of other popular Apps), where you can simply swipe to the right from anywhere on the screen to get back to the menu (or any other View Controller that initially pushed the one we are looking at).
I tried this code:

    @objc func swipeLeft(_ sender: UIPanGestureRecognizer) {
    let point = sender.translation(in: view)
    containerView.center = CGPoint(x: point.x > 0 ? view.center.x + point.x : view.center.x, y: view.center.y)
    if sender.state != .ended { return }

    if containerView.center.x < view.frame.width / 2 {
        dismissSelf()
    }
    else {
        UIView.animate(withDuration: 0.2) {
            self.containerView.center = self.view.center
        }
    }
}

UIPanGestureRecognizer,如果您present设置了ViewController,则效果很好,但是当按下ViewController时效果不佳.至少现在不是这样.

and a UIPanGestureRecognizer, which does work nicely if you presented your ViewController but not when it is pushed. At least not how it is right now.

现在,您看到一个黑色视图,这也是在推送的UIViewController底部的调试视图层次"中看到的.

Right now, you see a black view and that's also what you see in the "Debug View Hirachy" at the bottom of a pushed UIViewController.

感谢您的帮助!

推荐答案

我认为您要查找的内容已经内置在

I think what you are looking for is already built-in with the interactivePopGestureRecognizer

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true

如果您想制作一些自定义或其他动画,那么我认为您需要检查过渡.这是一篇进行自定义转换的好文章: https://medium.com/swift2go/simple-custom-uinavigationcontroller-transitions- fdb56a217dd8

if you want to make some custom or different animation then I think you need to check transitions. Here's a good article to make custom transitions: https://medium.com/swift2go/simple-custom-uinavigationcontroller-transitions-fdb56a217dd8

这篇关于UIPanGestureRecognizer弹出UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆