LongpressGesture 结束不会关闭视图

LongpressGesture end won't dismiss view

我创建了 LongPressGestureRecognizer 它应该打开弹出窗口并在发布时 dismiss 它。但是,由于某种原因,它没有 dismiss。这可能是什么原因造成的?

我是这样做的:

func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
        let brandInfoVC = BrandInfoViewController(nibName: "BrandInfo", bundle: nil)


        // Create the dialog
        let popup = PopupDialog(viewController: brandInfoVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)

        if longPressGestureRecognizer.state == UIGestureRecognizerState.began {

            let touchPoint = longPressGestureRecognizer.location(in: self.view)
            if let indexPath = tableView.indexPathForRow(at: touchPoint) {
                print("LongPressed cell", brands[indexPath.row])

                // Present dialog
                self.present(popup, animated: true, completion: nil)

            }
        }else if longPressGestureRecognizer.state == UIGestureRecognizerState.ended{
            print("LongPress released")//It does this
            popup.dismiss()// But it doesn't do this
        }

    }

应该可以。一定有其他你没有描述的事情发生。作为一个非常简单的测试,我实现了这个代码:

class ViewController: UIViewController {
    @IBAction func longPress(_ sender : UILongPressGestureRecognizer) {
        switch sender.state {
        case .began:
            self.definesPresentationContext = true
            let vc = UIViewController()
            vc.view.backgroundColor = .red
            vc.modalPresentationStyle = .custom
            vc.transitioningDelegate = self
            self.present(vc, animated:true)
        case .ended:
            self.dismiss(animated: true)
        default:break
        }
    }
}

class MyPresentationController : UIPresentationController {
    override var frameOfPresentedViewInContainerView: CGRect {
        return CGRect(x: 60, y: 200, width: 200, height: 200)
    }
}

extension ViewController : UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, 
        presenting: UIViewController?, source: UIViewController) 
        -> UIPresentationController? {
            return MyPresentationController(presentedViewController: presented, 
                                            presenting: presenting)
    }
}

这是我按住长按手势识别器所连接的视图,然后松开时得到的结果: