1

我有一个包含列表的 ProfileVC。我可以单击任何行单元格将显示 peek and 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)
    }
    
}

然后,在我注册的同一个文件ProfileVC.swiftviewDidLoad()

if (self.traitCollection.forceTouchCapability == .available){
    print("-------->", "Force Touch is Available")
    registerForPreviewing(with: self, sourceView: view)
}
else{
    print("-------->", "Force Touch is NOT Available")
}

结果

我不知道为什么我不能点击第四个单元格。

该行的最后一个单元格不会触发 Peek & Pop。

如何进行并进一步调试呢?

4

1 回答 1

1

您正在将视图控制器的根注册view为 peek 上下文的源视图。因此,CGPoint传递给 previewingContext(_ viewControllerForLocation:)` 的 ` 位于该视图的坐标空间中。

当您尝试从表视图中检索相应的行时,该点实际上会frame根据表视图在根视图中的相对位置从表视图中的相应点偏移。

这个偏移量意味着无法为表中的最后一行检索到相应的行;indexPathForRow(at:)返回nil并且您的函数返回而不做任何事情。

您可能还会发现,如果您强制触摸单元格的底部,您实际上可以看到下一行。

您可以将其CGPoint转换为表格视图的框架,但在注册预览时将表格视图指定为源视图会更简单:

if (self.traitCollection.forceTouchCapability == .available){
    print("-------->", "Force Touch is Available")
    registerForPreviewing(with: self, sourceView: self.profileTableView)
}
else{
    print("-------->", "Force Touch is NOT Available")
}
于 2019-01-18T23:17:20.227 回答