This article teaches you how to add 3D Touch functions and detailed writing methods to Table View through examples. Let’s test it.
Peek & Pop is a very practical hardware-related feature in the iPhone, which can not only improve operational efficiency, but also have clear visual expression.
Peek & Pop is a combination of two processes. Peek means tapping the screen to activate the preview window (will you think of the prompts given when the mouse swipes over the link in the computer, but it is more visually richer here). Pop means continuing to press the screen to open the preview window just now. If you just tap the screen, the preview window will disappear as you pick it up.
The easiest way to implement this function is to create a Segue through Storyboard (except for Segue in manual trigger mode) and check Peek & Pop on the Segue. However, if you want to customize the presentation process, you have to display it through encoding.
One of the key points to implementing this function through encoding is to provide a view object for preview, and there are 3 different ways to provide this object (not mentioned in general tutorials):
Create and initialize a view controller with complete program encoding.
Design the view through XIB, customize the view controller class file, and initialize the view through the init(nibName:bundle:) method.
Design the view through Storyboard, and then initialize the view through Storyboard's instantiateViewController method.
Other tutorials on the Internet are basically described Method 1 (including the easiest method mentioned at the beginning), and Method 2 and 3 are almost not mentioned. Methods 2 and 3 are also the most prone to errors.
I won’t talk about the process of creating a view. You can use any method, the focus is on initializing it. Suppose the view controller class name we created is: PreviewingViewController.
Method 1 Just use a custom initialization method directly (the initialization method can even be written without writing), the most typical example is: PreviewingViewController().
Methods 2 and 3 If you still use PreviewingViewController(), then you will wait for an error and keep looking for problems. The author spent a lot of time here at that time. Because methods 2 and 3 are views created through UI files, their initialization methods can only use specific and standard. Specifically:
Views created using the XIB method must be initialized using the init(nibName:bundle:) method.
Views created through Storyboard are initialized using the instantiateViewController method.
After mastering the above initialization method, it will be simple next. Complete the Peek & Pop in a total of three steps. Assuming the source view is a MainViewController, the PreviewingViewController is to preview:
Let MainViewController comply with the UIViewControllerPreviewingDelegate protocol and register Peek & Pop in its viewDidLoad() method:
if == .available {
registerForPreviewing(with: self, sourceView: tableView) // sourceView Use the view that needs to be triggered
}
Add proxy method to provide previewed view: previewingContext(_:viewControllerForLocation:):
// Peek operationfunc previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { // Get the pressed Cell guard let indexPath = (at: location), let cell = (at: indexPath) else { return nil } // Focus when pressed Cell // The areas to focus when pressing can be customized, just provide what you need. = // According to the above discussion, a corresponding initialization method is provided, here taking storyboard as an example. let previewVC = ?.instantiateViewController( withIdentifier: "xxx") as! PreviewingViewController // Pass the information required for preview = return previewVC }
Add a proxy method to open the preview view: previewingContext(_:commit:):
// Pop operationfunc previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { // The conditional judgment used here allows you to not trigger the Pop operation in some cases. if xxx { show(viewControllerToCommit, sender: self) //Select show or present according to the way you open the view // present(viewControllerToCommit, animated: true) } }