我有一个包含列表的ProfileVC。我可以点击任何行单元格来显示peek和pop功能。
ProfileVC.swift
我添加了扩展名
extension ProfileViewController : UIViewControllerPreviewingDelegate {
func detailViewController(for indexPath: IndexPath) -> ProfileDetailViewController {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "ProfileDetailViewController") as? ProfileDetailViewController else {
fatalError("Couldn't load detail view controller")
}
let cell = profileTableView.cellForRow(at: indexPath) as! ProfileTableViewCell
// Pass over a reference to the next VC
vc.title = cell.profileName?.text
vc.cpe = loginAccount.cpe
vc.profile = loginAccount.cpeProfiles[indexPath.row - 1]
consoleLog(indexPath.row - 1)
//print("3D Touch Detected !!!",vc)
return vc
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let indexPath = profileTableView.indexPathForRow(at: location) {
// Enable blurring of other UI elements, and a zoom in animation while peeking.
previewingContext.sourceRect = profileTableView.rectForRow(at: indexPath)
return detailViewController(for: indexPath)
}
return nil
}
//ViewControllerToCommit
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
// Push the configured view controller onto the navigation stack.
navigationController?.pushViewController(viewControllerToCommit, animated: true)
}
}
然后,在viewDidLoad()
的同一文件ProfileVC.swift中我注册了它
if (self.traitCollection.forceTouchCapability == .available){
print("-------->", "Force Touch is Available")
registerForPreviewing(with: self, sourceView: view)
}
else{
print("-------->", "Force Touch is NOT Available")
}
我不知道为什么我不能点击第4个单元格。
该行的最后一个单元格不会触发Peek&Pop。
如何进行并进一步调试?
您正在注册视图控制器的根view
作为查看上下文的源视图。因此,传递给previewingContext(_ viewControllerForLocation :)的`的CGPoint
位于该视图的坐标空间中。
当您尝试从表视图中检索相应的行时,该点实际上将基于根视图中表视图的相对位置从表视图的frame
中的对应点偏移。
此偏移意味着无法为表中的最后一行检索相应的行; indexPathForRow(at:)
返回nil
,你的函数返回而不做任何事情。
您可能还会发现,如果您强行触摸单元格的底部,您实际上可以查看下一行。
您可以将CGPoint
转换为表格视图的框架,但只需在注册预览时将tableview指定为源视图即可:
if (self.traitCollection.forceTouchCapability == .available){
print("-------->", "Force Touch is Available")
registerForPreviewing(with: self, sourceView: self.profileTableView)
}
else{
print("-------->", "Force Touch is NOT Available")
}