Tusker/Tusker/Screens/Utilities/Previewing.swift

39 lines
1.5 KiB
Swift

//
// PreviewViewControllerProvider.swift
// Tusker
//
// Created by Shadowfacts on 10/10/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import SafariServices
protocol PreviewViewControllerProvider {
func getPreviewViewController(forLocation location: CGPoint, sourceViewController: UIViewController) -> UIViewController?
}
@objc extension UITableViewController: UIViewControllerPreviewingDelegate {
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let indexPath = tableView.indexPathForRow(at: location),
let cell = tableView.cellForRow(at: indexPath) as? UITableViewCell & PreviewViewControllerProvider {
let cellLocation = cell.convert(location, from: tableView)
if let vc = cell.getPreviewViewController(forLocation: cellLocation, sourceViewController: self) {
// previewingContext.sourceRect = tableView.rectForRow(at: indexPath)
return vc
}
}
return nil
}
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
if viewControllerToCommit is LargeImageViewController || viewControllerToCommit is SFSafariViewController {
present(viewControllerToCommit, animated: false)
} else {
navigationController!.pushViewController(viewControllerToCommit, animated: false)
}
}
}