我创建了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
}
}
答案 0 :(得分:1)
它应该工作。必须有一些你没有描述过的事情。作为一个非常简单的测试,我实现了这段代码:
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)
}
}
当我按住连接长按手势识别器的视图时,这就是我得到的,然后当我发布时: