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

时间:2017-07-03 09:06:50

标签: ios swift3 uipresentationcontroller uipresentingcontroller

我有一个CustomPresentationController可以使用自定义动画制作动画;

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

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

我想要完成的是检测presentedViewController之外的水龙头,可能在灰色区域内,因此我可以忽略presentedViewController,就像解雇ActionSheet一样但我没能做到。让我解释一下我到目前为止所尝试的内容。

我尝试在阴影灰色视图中添加UITapGestureRecognizer,但由于我提供了不同的控制器,因此应用引擎可能会认为因为阴影视图不在顶部层次结构视图它可能无法访问,因此它会阻止'识别器 - 每当我点击它时,手势处理都不会触发。

我现在正在实施另外一个向下轻扫以解雇,我可以很容易地解决这个问题,但我真的希望这个外部功能也能正常工作。

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

应用图片如下:

screenshot

5 个答案:

答案 0 :(得分:3)

我的解决方案:

在呈现视图控制器(又名ViewControllerA)时:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
                
let vcb = storyboard.instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB // ViewControllerB is the presented view controller 
        
vcb.modalPresentationStyle = .custom
        
vcb.transitioningDelegate = self

modalRatio = Float(0.5) // modalRatio is an object property 
   
self.present(pvc, animated: true)

ViewControllerA还应实现过渡委托:

extension ViewControllerA: UIViewControllerTransitioningDelegate {
        
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
                
        return PartialSizePresentController(presentedViewController: presented, presenting: presenting, withRatio: modalRatio ?? 0.5) // modal ratio is configurable using modalRatio property
        
    }
    
}

然后,实现表示控制器(也称为PartialSizePresentController),以便它也可以处理轻击手势:

class PartialSizePresentController: UIPresentationController {
    
    let heightRatio : CGFloat
    
    init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?, withRatio ratio: Float = 0.5) {
        
        heightRatio = CGFloat(ratio)
        
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
        
    }

    override var frameOfPresentedViewInContainerView: CGRect {
        
        guard let cv = containerView else { fatalError("No container view available") }
        
        return CGRect(x: 0, y: cv.bounds.height * (1 - heightRatio), width: cv.bounds.width, height: cv.bounds.height * heightRatio)
        
    }
    
    override func presentationTransitionWillBegin() {
        
        let bdView = UIView(frame: containerView!.bounds)
        
        bdView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        containerView?.addSubview(bdView)
        
        bdView.addSubview(presentedView!)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PartialSizePresentController.handleTap(_:)))
        
        bdView.addGestureRecognizer(tapGesture)
        
    }
    
    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        
        let vcb = presentedViewController as! ViewControllerB
        
        vcb.dismiss(animated: true, completion: nil)

    }
}

答案 1 :(得分:1)

您使用UITapGestureRecognizer走在正确的轨道上。只需确保您实现shouldRecognizeSimultaneouslyWith

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

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

答案 2 :(得分: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) 
   } 

如果您正在使用情节提要,请确保将隐形按钮放在自定义视图下。

我希望这会有所帮助。

答案 3 :(得分:1)

// 对我有用

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    var touch: UITouch? = touches.first
    if touch?.view == yourView {
        navigationController?.popViewController(animated: true)
        dismiss(animated: true, completion: nil)
    }
}

答案 4 :(得分:0)

尝试使用以下代码:

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

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

希望这会奏效。 :d