简体   繁体   中英

LongpressGesture end won't dismiss view

I created LongPressGestureRecognizer which should open popup and on release dismiss it. However it doesn't dismiss it for some reason. What could cause this?

I do it like this:

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
        }

    }

It should work. There must be something else going on that you have not described. As a very simple test, I implemented this code:

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

And this is what I get when I hold down on the view to which the long press gesture recognizer is attached, and then when I release:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM