一、图标上的3DTouch
1、动态加载自定义的ShortcutItem
if (application.shortcutItems.count == 0) {
UIMutableApplicationShortcutItem *itemThor =[[UIMutableApplicationShortcutItem alloc]initWithType:[NSString stringWithFormat:@"%@.second",[[NSBundle mainBundle] bundleIdentifier]] localizedTitle:@"雷神" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLocation] userInfo:nil];
UIMutableApplicationShortcutItem *itemBlack =[[UIMutableApplicationShortcutItem alloc]initWithType:[NSString stringWithFormat:@"%@.third",[[NSBundle mainBundle] bundleIdentifier]] localizedTitle:@"黑寡妇" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLocation] userInfo:nil];
UIMutableApplicationShortcutItem *itemCaptain =[[UIMutableApplicationShortcutItem alloc]initWithType:[NSString stringWithFormat:@"%@.fourth",[[NSBundle mainBundle] bundleIdentifier]] localizedTitle:@"美国队长" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLocation] userInfo:nil];
application.shortcutItems = @[itemBlack,itemCaptain,itemThor];
}
2、plist中添加
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler{
[_viewController.navigationController popToRootViewControllerAnimated:NO];
_viewController.shortcutName =shortcutItem.localizedTitle;
[[NSNotificationCenter defaultCenter] postNotificationName:@"ShortCut" object:nil];
}
二、视图上的3DTouch
1、查看当前设备是否支持3DTouch:
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:self.tableViewList]; //self.tableViewList 你将要点击的view
}
else {
NSLog(@"该设备不支持3D-Touch");
}
2、实现UIViewControllerPreviewingDelegate:
2.1- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext
viewControllerForLocation:(CGPoint)location
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath * indexPath =[_tableViewList indexPathForRowAtPoint:location];
UITableViewCell * cell = [_tableViewList cellForRowAtIndexPath:indexPath];
if (!cell) {
return nil;
}
DetailViewController *detailVC =[[DetailViewController alloc]initWithTitle:[_arrayData objectAtIndex:indexPath.row]];
detailVC.preferredContentSize = CGSizeMake(0, 0);
previewingContext.sourceRect = cell.frame;
return detailVC;
}
2.2- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext
commitViewController:(UIViewController *)viewControllerToCommit
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self.navigationController pushViewController:viewControllerToCommit animated:NO];
}
3.上移视图出现选项
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction *action = [UIPreviewAction actionWithTitle:@"赞" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
return @[action];
}