I don’t know if you have found wechat chat history list, Apple’s own contacts history and other applications such as the hidden long press function. Ha ha, if not, don’t be discouraged and don’t doubt life. You need to change your phone or upgrade your system!

This feature looks pretty cool, but also pretty handy, if you look at history and other shortcuts as an entry point. If you don’t think of the right way to implement it, it might be difficult to implement, or it might not work as well. It’s actually quite simple to implement if you find the right way.

In fact, this effect is the use of Apple 3Dtouch, 3Dtouch does not support mobile phones or systems can not experience.

Sold a big mystery, also a solo meal, let me see how to implement. Ha ha.

Register 3DTouch behavior for the desired UIView

[self registerForPreviewingWithDelegate:self sourceView:cell];Copy the code

This method is actually a UIViewController method, and the last parameter is to specify the View to which 3DTouch is to be added, so here we’re going to add our own TableViewCell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYTableViewCell" forIndexPath:indexPath];
    [self registerForPreviewingWithDelegate:self sourceView:cell];
    return cell;
}Copy the code

implementationUIViewControllerPreviewingDelegateagreement

There are two methods to this protocol that we’re going to implement

  • The first one- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    PreViewController *vc = [[PreViewController alloc] init];
    return vc;
}Copy the code

This protocol method is our 3DTouch after the display of the view, want to display their own implementation, can completely control their own

  • The second- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    [self showViewController:viewControllerToCommit sender:self];
}Copy the code

The protocol method is to display our view


Gorgeous dividing line, basically at this point to achieve the effect of long press to pop up the view, careful people may notice that there is actually another effect is long press to pop up the view can slide up to appear, after a few buttons appear. Now let’s see how to add those buttons.

It’s actually pretty easy to add these buttons. Go to the PreViewController class that we defined earlier, and rewrite it -(NSArray

> *)previewActionItems is also a method that comes with UIViewController itself.

-(NSArray<id<UIPreviewActionItem>> *)previewActionItems {UIPreviewAction *p1 = [UIPreviewAction actionWithTitle:@" Delete" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {NSLog(@" click delete ");}]; UIPreviewAction * p2 = [UIPreviewAction actionWithTitle: @ "undo" style: UIPreviewActionStyleSelected Handler :^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {NSLog(@" click undo ");}]; return @[p1,p2]; }Copy the code