After the 6s and 6s Plus, pressing hard against the app icon will trigger 3DTouch.

Step 1:3DTouch device support check:
Check whether the current device supports 3DTouch
/ / there is a new enumeration in iOS9 typedef NS_ENUM (NSInteger UIForceTouchCapability) {UIForceTouchCapabilityUnknown = 0, / / the support of the unknown attribute UIForceTouchCapabilityUnavailable = 1, / / does not support UIForceTouchCapabilityAvailable = 2 / / support};Copy the code
We typically do this during the lifetime of each ViewController:
Defines a BOOL property that is supported or not by the device
@property (nonatomic , assign) BOOL support3DTouch;
Copy the code
Check for support in the lifecycle function
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Check whether the current support for 3DTouch self.support3DTouch = [self support3DTouch]; }Copy the code
Check for support outside the lifecycle (changes due to possible lifecycle functions)
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection NS_AVAILABLE_IOS(8_0) {
self.support3DTouch = [self support3DTouch];
}
Copy the code
Check whether the 3DTouch method is supported
- (BOOL)support3DTouch {// If 3DTouch is enabledif (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
return YES;
}
returnNO; }}Copy the code
Step 2: Configure the shortcut view list
Create shortcut list view there are two ways: 1, one kind is to edit the info. The file of UIApplicationShortcutItems, through visual interface to add keys to direct configuration info. Plist

2. The other option is to add items to the project’s appdelegate.m using code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]]; [self.window makeKeyAndVisible]; // Code to create a shortcut view list method, [self create3DTouchShotItems];returnYES; } - (void) create3DTouchShotItems {/ / create a shortcut icon of the item UIApplicationShortcutItemIconFile UIApplicationShortcutIcon * icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon2"];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon3"]; / / create the quick and the item the userinfo UIApplicationShortcutItemUserInfo NSDictionary * info1 = @ {@"url": @"url1"};
NSDictionary *info2 = @{@"url": @"url2"};
NSDictionary *info3 = @{@"url": @"url3"}; / / create ShortcutItem UIMutableApplicationShortcutItem * item1 = [[UIMutableApplicationShortcutItem alloc] initWithType: @"XS_3DTocuh_1" localizedTitle:@"Sweep it." localizedSubtitle:@"" icon:icon1 userInfo:info1];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_2" localizedTitle:@"smile" localizedSubtitle:@"Face life with a smile" icon:icon2 userInfo:info2];
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_3" localizedTitle:@"Shopping" localizedSubtitle:@"Shopping" icon:icon3 userInfo:info3];
NSArray *items = @[item1, item2, item3];
[UIApplication sharedApplication].shortcutItems = items;
}
Copy the code
Step 3: Register 3DTouch events for cells in the list view
1, first of all, on the front page of the current controller in comply with UIViewControllerPreviewingDelegate 2 UIViewControllerPreviewingDelegate agreement, judge whether the device supports before registration (i.e., the first step)
3, registration: [self registerForPreviewingWithDelegate: self sourceView: cell];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ZLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLTableViewCell"];
if(cell == nil) { cell = [ZLTableViewCell cellWithTableView:tableView]; } cell.dataFrame = self.dataSource[indexPath.row]; // Register an agent for the cell to support 3DTouch gesturesif (self.support3DTouch) {
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
return cell;
}
Copy the code
Step # # # 4: complete callback UIViewControllerPreviewingDelegate agreement, achieve Peek Pop the current controller on the front page,
#pragma mark - 3DTouch UIViewControllerPreviewingDelegate
Copy the code
Peek implementation code:
// When you tap the control, Out of peek proxy method - (UIViewController *) previewingContext: (id < UIViewControllerPreviewing >) previewingContext ViewControllerForLocation: (CGPoint) location {/ / prevent repetition to joinif ([self.presentedViewController isKindOfClass:[ZLPeekViewController class]])
{
return nil;
}
else
{
ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
ZLCellData * cellData = cell.dataFrame.cellData;
ZLPeekViewController *peekViewController = [[ZLPeekViewController alloc] init];
peekViewController.cellData = cellData;
peekViewController.delegate = self;
returnpeekViewController; }}Copy the code
Pop code
// This method is used when repressing peek, Jump into pop proxy method - (void) previewingContext: (id < UIViewControllerPreviewing >) previewingContext commitViewController:(UIViewController *)viewControllerToCommit { ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView; ZLCellData * cellData = cell.dataFrame.cellData; ZLPopViewController *popViewController = [[ZLPopViewController alloc] init]; popViewController.cellData = cellData; / / show in the form of prentViewController [self showViewController: popViewController sender: self]. / / show / / as a push [self. The navigationController pushViewController: popViewController animated: YES]; }Copy the code
### step 5: Slide up the button configuration method under Peek
In zlPeekViewController.m, implement – (NSArray> *)previewActionItems callback method
#pragma mark - Preview Actions- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {// Generates UIPreviewAction UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 1 selected");
[self.delegate pushToPopViewControllerWithCellData:self.cellData];
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Event 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 2 selected");
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Action 3 selected");
}];
UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"Button 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 1 selected");
}];
UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"Button 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 2 selected");
}];
UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"Button 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"tap 3 selected");
}];
NSArray *actions = @[action1, action2, action3];
NSArray *taps = @[tap1, tap2, tap3];
UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"A set of events" style:UIPreviewActionStyleDefault actions:actions];
UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"A set of buttons"style:UIPreviewActionStyleDefault actions:taps]; NSArray *group = @[group1,group2]; // You can also return an array of three separate action objects instead of groupsreturn group;
}
Copy the code
Now you can test it and see the effect. If you need a demo, you can go to my GitHub to download ~
