iOS - 关闭呈现的视图控制器触摸其视图外部

问题描述 投票:1回答:3

我有一个CustomPresentationController,用自定义动画制作动画;

这个特定的控制器出现了,更多的是屏幕尺寸的50%,当我呈现它时,我为presentingViewController添加了一个阴影灰色视图,因此它增加了一些深度。

如果我点击presentedViewController中的cancel按钮,我只能忽略NavBar,我称之为默认的dismiss(:)方法。

我想要完成的是检测presentedViewController外面的水龙头,可能在灰色区域内,所以我可以解雇presentedViewController,不知何故就像解雇ActionSheet但我没有做到这一点。让我解释一下到目前为止我尝试过的内容。

我试图在阴影灰色视图中添加一个UITapGestureRecognizer,但由于我提出了一个不同的控制器,app-engine可能会认为因为阴影视图不在顶层视图上,所以它可能无法访问,因此它会阻塞'识别器 - 每当我点击它时,手势柄都不会触发。

我现在正在实施另外一个轻扫以解除,我可以很容易地做到,但我真的希望tap-outside功能也能正常工作。

有关如何处理此问题的任何提示?

应用图片如下:

screenshot

ios swift3 uipresentationcontroller uipresentingcontroller
3个回答
1
投票

我只需要在我的一个应用程序中实现它。

我通过添加一个覆盖整个视图和按钮的按钮使其工作,一旦点击触发VC被解除。

添加按钮后,您可以在顶部添加自定义视图。

到目前为止看起来它的效果非常好。

我的代码如下(我以编程方式执行所有操作,无故事板)

    //—————————————————————————————
    // MARK: View Life Cycle
    //—————————————————————————————

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.clear //VC view background transparent
    setupUI()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    //Animate blackView opacity to 1 to give some depth
    UIView.animate(withDuration: 0.4, delay: 0.2, options: .curveEaseInOut, animations: {
        self.blackView.alpha = 1 
    })
}

    //————————————————
    // MARK: Setup UI
    //————————————————

    let blackView: UIView = {
        let view = UIView()
        view.alpha = 0.0
        view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        return view
    }()
    //Invisible button which covers the entire view that can be tapped 
    lazy var dismissLayerBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(tapToDismiss), for: .touchUpInside)
        return btn
    }()

    @objc func tapToDismiss() {
        print("tapToDimiss")
        self.dismiss(animated: true, completion: nil)
    }

    let milestonePickerView: MilestonePickerView = {
        let view = MilestonePickerView(frame: .zero)
        return view
    }()

    func setupUI() {

        view.addSubview(blackView)
        view.addSubview(dismissLayerBtn)
        view.addSubview(milestonePickerView) //Important to add the customView after the button.

        blackView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)

        dismissLayerBtn.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)

        milestonePickerView.anchor(top: nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 20, paddingBottom: 40, paddingRight: 20, width: 0, height: 400)

//I'm using a custom extension to setup constraints (anchors) 
   } 

如果您正在使用故事板,请确保将隐形按钮放在自定义视图下。

我希望这有帮助。


1
投票

你和UITapGestureRecognizer在正确的轨道上。只要确保你实现shouldRecognizeSimultaneouslyWith

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

这应该允许手势正确触发。


0
投票

试试我的以下代码:

你需要在你提供的控制器中实现这个方法,你正在使用它作为一个弹出窗口。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
   // Here write down you logic to dismiss controller     
}

希望这会奏效。 :d

© www.soinside.com 2019 - 2024. All rights reserved.