如何通过代码手动实现表中特定行的Peek&Pop Feature?

时间:2018-10-18 17:54:22

标签: ios swift uitableview force-touch

目标

我正在尝试将偷看和弹出功能添加到我的表格行


enter image description here

2 个答案:

答案 0 :(得分:2)

您没有从tableview获取单元格。因此,步骤如下:

  1. 使用indexPath方法中的定位点获取UIViewControllerPreviewingDelegate
  2. 使用indexPathtableView获取单元格。
  3. 获取单元格后,将单元格框架转换为表格视图框架。
  4. 将框架作为sourceRect赋予previewingContext

下面的代码:

viewDidLoad

if self.traitCollection.forceTouchCapability == .available {
    registerForPreviewing(with: self, sourceView: tableView)
}

扩展名:

extension ProfileViewController: UIViewControllerPreviewingDelegate {

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        if let indexPath = tableView.indexPathForRow(at: location), let cell = tableView.cellForRow(at: indexPath) {
            previewingContext.sourceRect = tableView.convert(cell.frame, to: self.tableView)
            guard let detailViewController = storyboard?.instantiateViewController(withIdentifier: "profileDetail") as? ProfileDetailViewController else {
                return nil
            }
            return detailViewController
        }
        return nil
    }

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        self.navigationController?.pushViewController(viewControllerToCommit, animated: true)
    }
}

答案 1 :(得分:1)

通常,我使用一个简单的单元格扩展名来做到这一点:

class ForceTouchCell: UITableViewCell {
    var previewingContext: UIViewControllerPreviewing?

    func registerPreview(on controller: UIViewController, with delegate: UIViewControllerPreviewingDelegate) {
        self.previewingContext = controller.registerForPreviewing(with: delegate, sourceView: self)
    }

    func unregisterPreview(on controller: UIViewController) {
        guard let previewingContext = previewingContext else {
            return
        }

        controller.unregisterForPreviewing(withContext: previewingContext)
        self.previewingContext = nil
    }
}

然后在控制器上:

class MyController: UIViewController, UITableViewDelegate {
    var tableView: UITableView!

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        guard traitCollection.forceTouchCapability != previousTraitCollection?.forceTouchCapability else {
            return
        }

        let visibleCells = tableView.visibleCells
               .compactMap { $0 as? ForceTouchCell }

        if traitCollection.forceTouchCapability == .available {
           visibleCells.forEach {
               $0.registerPreview(on: self, with: self)
           }
        } else {
           visibleCells.forEach {
               $0.unregisterPreview(on: self)
           }
        }
    }

    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let forceTouchCell = cell as? ForceTouchCell {
            if self.traitCollection.forceTouchCapability == .available {
                forceTouchCell.registerPreview(on: self, with: self)
            }
         }
    }

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let forceTouchCell = cell as? ForceTouchCell {
            forceTouchCell.unregisterPreview(on: self)
        }
    }
}

extension MyConroller: UIViewControllerPreviewingDelegate {
    func previewingContext(
    _ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint
) -> UIViewController? {
    guard
        let cell = (previewingContext.sourceView as? ForceTouchCell),
        let indexPath = tableView.indexPath(for: cell)
    else {
        return nil
    }

    // use the indexPath/cell to create preview controller
}

此解决方案的优点在于,它可以轻松地在各个控制器之间共享,例如使用具有默认实现的协议。