IOS adds 3D touch function to table view


In this paper, through examples to teach you how to add 3D touch function in table view and detailed writing, follow the test.

Peek & pop is a very practical hardware related feature in iPhone, which can not only improve the operation efficiency, but also have clear visual expression.

Peek & pop is a combination of two processes. Peek means to tap the screen to activate the preview window. Pop means to press the screen again to open the preview window. If you just tap the screen, the preview window will disappear with your fingers.

The simplest way to achieve this function is to create a segue (except for the segue in manual trigger mode) through storyboard, and check peek & pop on the segue. However, if you want to customize the presentation process, you need to display it by coding.

One of the key points to realize this function through coding is to provide a view object for preview. There are three different ways to provide this object (not mentioned in the general tutorial)

Create and initialize a view controller with complete program code.
Through Xib design view, custom view controller class file, and through init( nibName:bundle 🙂 method to initialize the view.
Design the view through storyboard, and then initialize the view through the instantiateviewcontroller method of storyboard.
Other online tutorials basically describe method 1 (including the simplest method mentioned at the beginning), but method 2 and 3 are hardly mentioned. Methods 2 and 3 are also the most error prone.

I don’t need to talk about the process of creating a view. You can use any method. The key is to initialize it. Suppose the name of the view controller class we create is previewingviewcontroller.

Method 1: just use the user-defined initialization method (the initialization method may not even be written). The most typical method is previewingviewcontroller().
Methods 2 and 3, if you still use previewing view controller (), you’ll be waiting to report errors and keep looking for problems. I spent a lot of time here at that time. Because methods 2 and 3 are all views created through UI files, their initialization methods can only be specific and standard. Specifically, it is:
For views created in Xib mode, init is used( nibName:bundle 🙂 method initialization.
The view created by storyboard should be initialized with the instantiateviewcontroller method.
After mastering the above initialization method, it’s easy to complete peek & pop in three steps. Suppose that the source view is mainviewcontroller and the previewingviewcontroller is previewed

Let mainviewcontroller comply with uiviewcontrollerpreviewingdelegate protocol, and register peek & Pop: in its viewdidload() method
if traitCollection.forceTouchCapability == .available {
Register for previewing (with: self, sourceview: tableview) / / sourceview can use the view that needs to be triggered
Add the preview view provided by proxy method: previewingcontext (_ :viewControllerForLocation:):

//Peek operation
func previewingContext(_ previewingContext: UIViewControllerPreviewing,
            viewControllerForLocation location: CGPoint) -> UIViewController?
  //Get the pressed cell
  let indexPath = tableView.indexPathForRow(at: location),
  let cell = tableView.cellForRow(at: indexPath)
  else {
    return nil
  //Focus cell while pressing
  //When pressing, the focus area can be customized to provide what you need.
  previewingContext.sourceRect = cell.frame

  //According to the above discussion, the corresponding initialization method is provided. Here, storyboard is taken as an example.
  let previewVC = self.storyboard?.instantiateViewController(
    withIdentifier: "xxx") as! PreviewingViewController
  //Pass on the information needed for preview =
  return previewVC

Add proxy method to open preview view: previewingcontext (_ :commit:):

//Pop operation
func previewingContext(_ previewingContext: UIViewControllerPreviewing,
              commit viewControllerToCommit: UIViewController)
  //The condition judgment used here allows you not to trigger pop operation in some cases.
  if xxx {
    show(viewControllerToCommit, sender: self)
    //Select show or present according to the way to open the view
    // present(viewControllerToCommit, animated: true)