简体   繁体   English

速览UIView显示SFSafariViewController

[英]Peek-and-Pop UIView to show SFSafariViewController

I have a UITableView and in each cell theres a UIView added as subview. 我有一个UITableView并在每个单元格中都有一个UIView作为子视图添加。 What I currently have is a UITapGestureRecognizer to open the In-App-Safari whenever I click one of those UIViews . 我现在已经是一个UITapGestureRecognizer打开In-App-Safari ,每当我点击其中的一个UIViews However I'm struggling to implement Apples sample code of the peek and pop feature for force touch. 但是,我正在努力实现Apple的peek和pop功能的示例代码以实现强制触摸。 My code looks as follows: 我的代码如下所示:

 @IBOutlet var mainTableView: UITableView!

in viewDidLoad() : viewDidLoad()中

 registerForPreviewing(with: self as! UIViewControllerPreviewingDelegate, sourceView: mainTableView)

after viewDidLoad() : viewDidLoad()之后

 func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
            // First, get the index path and view for the previewed cell.
        guard let indexPath = tableView.indexPathForRow(at: location),
              let cell = tableView.cellForRow(at: indexPath)
              else { return nil }

 //Enable blurring of other UI elements, and a zoom in animation while peeking.

 previewingContext.sourceRect = cell.frame

 //Create and configure an instance of the color item view controller to show for the peek.

 guard let url = someURL else { return nil }
            let vc = SFSafariViewController(url: url)
            vc.delegate = self as SFSafariViewControllerDelegate

            return vc
        }

        func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) 
{

//Push the configured view controller onto the navigation stack.

 navigationController?.pushViewController(viewControllerToCommit, animated: true)
}

Xcode returns errors such as Xcode返回错误,例如

Received memory pressure event 4 vm pressure 1 收到内存压力事件4 vm pressure 1

and

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController previewingContext:viewControllerForLocation:]: unrecognized selector sent to instance 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[ViewController PreviewingContext:viewControllerForLocation:]:无法识别的选择器已发送至实例

I haven't found a solution yet to peek and pop web content when pressing a UIView . 按下UIView时,我还没有找到窥视和弹出Web内容的解决方案。

Ok, so i found out that I simply forgot to add UIViewControllerPreviewingDelegate to my class: 好的,所以我发现我只是忘记将UIViewControllerPreviewingDelegate添加到我的课程中:

class MyViewController: UITableViewController, UIViewControllerPreviewingDelegate, SFSafariViewControllerDelegate

The peek works as intended with the stated method, but for the pop to work I had to adapt my last function: 窥视按规定的方法按预期工作,但要使流行音乐起作用,我必须调整我的最后一个功能:

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    show(viewControllerToCommit, sender: self)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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