Swift3:后退手势

Swift 3: Back gesture

我的 Swift 3 应用程序中有一个包含 UITableView 的 homeViewController。单击 table 的单元格时,我会显示 detailViewController。我使用 addChildViewController(vc) 将其显示为子项。我想实现后退手势以便 return 到 homeViewController。我希望在设备的默认邮件应用程序中获得与返回模式相同的结果(例如,当我在收件箱中时,返回模式为 return 到邮件的主页)。换句话说,该手势允许用户从左向右拖动视图,然后继续从左向右拖动直到显示主视图,或者从右向左拖动视图以在整个屏幕中保留详细视图.我尝试使用 UIPanGestureRecognizer 来做到这一点,但我的视图左右移动,有时它从右侧显示主视图。所以简而言之,我想要的结果与邮件应用程序的后退手势功能相同。

这是我的代码:

    var rightSwipe: UIPanGestureRecognizer = UIPanGestureRecognizer()
        self.rightSwipe = UIPanGestureRecognizer(target: self, action: #selector(self.handleSwipes(_:)))
        self.view.addGestureRecognizer(rightSwipe)
 func handleSwipes(_ sender: UIPanGestureRecognizer){

        if self.rightSwipe.state == .began || self.rightSwipe.state == .changed {

            let translation = rightSwipe.translation(in: self.view)
            // note: 'view' is optional and need to be unwrapped
            //print("translation is : \(translation)")
            if(translation.x > 0 && translation.y == 0.0){
            self.rightSwipe.view!.center = CGPoint(x: self.rightSwipe.view!.center.x + translation.x, y: self.rightSwipe.view!.center.y + translation.y)
            rightSwipe.setTranslation(CGPoint.zero, in: self.view)
            }

        }
    }

要在故事板中使用 NavigationController,请将 HomeViewController 嵌入 UINavigationController。要嵌入导航控制器 select 您的 HomeViewController,请在菜单中选择 Editor > Embed In > Navigation Controller

现在 didSelectRowAt indexPath 推送 detailViewController

self.navigationController?.pushViewController(detailVC, animated: true)

如果手势仍然无效,那么在 HomeViewControllerviewDidLoad 中设置这个。

self.navigationController?.interactivePopGestureRecognizer.isEnabled = true