斯威夫特3:背部姿势

时间:2016-11-25 16:28:12

标签: ios swift swift3 uipangesturerecognizer

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

这是我的代码:

    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)
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

要在故事板中使用NavigationController,请将HomeViewControllerUINavigationController一起嵌入。要嵌入navigationController,请选择HomeViewController,在菜单中选择Editor > Embed In > Navigation Controller

现在在didSelectRowAt indexPath推送detailViewController

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

如果静止手势不起作用,则在HomeViewController viewDidLoad设置此手势。

self.navigationController?.interactivePopGestureRecognizer.isEnabled = true