【发布时间】:2019-07-10 08:49:50
【问题描述】:
我有一个UITableView,在每个单元格中都有一个UIView 添加为子视图。我目前拥有的是UITapGestureRecognizer,每当我单击其中一个UIViews 时,就可以打开In-App-Safari。但是,我正在努力实现 Apples sample code 的 peek and pop 功能以进行强制触摸。我的代码如下所示:
@IBOutlet var mainTableView: UITableView!
在viewDidLoad()中:
registerForPreviewing(with: self as! UIViewControllerPreviewingDelegate, sourceView: mainTableView)
在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 返回错误如
收到内存压力事件4 vm压力1
和
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[ViewController previewingContext:viewControllerForLocation:]: unrecognized selector sent to instance
我还没有找到在按下UIView 时查看和弹出网页内容的解决方案。
【问题讨论】:
-
3D touch 已从 2019 年的 iPhone 中移除
-
我仍然认为这是一个不错的功能 ;)