3D touch: pop controller without peeping

I have a list of some of the content elements that it represents, and a controller for the parameters for each element. I present this 3D touch option controller or long tap for older devices. I implemented it very simply:

extension ViewController {
    func checkForForceTouch() {
        if #available(iOS 9.0, *) {
            if traitCollection.forceTouchCapability == .available {
                registerForPreviewing(with: self, sourceView: tableView)
            } else {
                addLongPressRecognizer()
            }
        } else {
            addLongPressRecognizer()
        }
    }

...
}

@available(iOS 9.0, *)
extension ViewController: UIViewControllerPreviewingDelegate {
    public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        if let indexPath = tableView.indexPathForRow(at: location) {
            let cell = tableView.cellForRow(at: indexPath)!
            previewingContext.sourceRect = cell.frame

            return self.optionsController(for: indexPath)
        } else {
            return nil
        }
    }

    public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        viewControllerToCommit.modalPresentationStyle = .overFullScreen
        present(viewControllerToCommit, animated: true, completion: nil)
    }
}

Everything works fine, except for one: in the case of 3D-touch options, the controller is displayed in view mode. I want to introduce it right away as an Apple Music app. Is there any way to do this?

+4
source share

Source: https://habr.com/ru/post/1661062/


All Articles