尽管3DTouch已经出来很久,但是项目最近才接触到,所以就整理了一下自己实现的代码,记录。
1.实现重按app,弹出窗口(如图所示)
在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中实现:
//添加3dtouch //动态加载自定义的ShortcutItem if (application.shortcutItems.count == 0) { UIMutableApplicationShortcutItem *itemScan = [[UIMutableApplicationShortcutItem alloc]initWithType:@"Scan" localizedTitle:@"扫一扫" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"icon2"] userInfo:nil]; UIMutableApplicationShortcutItem *itemWrite = [[UIMutableApplicationShortcutItem alloc]initWithType:@"listen" localizedTitle:@"去写作" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"] userInfo:nil]; application.shortcutItems = @[itemScan,itemListen]; } /*ps:about icon [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"]//这是替换自己的图片 [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose];//这是系统内的特定icon*/
随后实现:
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler;
在该方法中,可以通过判断唯一标示符(之前设置的initwithtype:)或者判断localizedTitle来区别点击的哪一行
而后可以使用通知或者直接导航等等跳转到你想要去的界面,这里就不细说了。
2.重按cell的3DTouch效果
实现
UIViewControllerPreviewingDelegate代理方法
- (void)viewDidLoad { [self registerPreview]; } -(void)registerPreview{ //判断是否支持3dtouch,设置委托代理 if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { [self registerForPreviewingWithDelegate:self sourceView:self.tableView]; } else { NSLog(@"该设备不支持3D-Touch"); } } #pragma mark - UIViewControllerPreviewingDelegate - (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { NSIndexPath * indexPath =[_tableView indexPathForRowAtPoint:location]; UITableViewCell * cell = [_tableView cellForRowAtIndexPath:indexPath]; //以上得到你点击的哪一行的cell if (!cell) { return nil; } //显示3dtouchpreviewViewcontroller界面 3DTouchPreviewViewController *detailVC =[[3DTouchPreviewViewController alloc]init]; detailVC.preferredContentSize = CGSizeMake(0, 0); previewingContext.sourceRect = cell.frame; return detailVC; } - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { [self.navigationController pushViewController:viewControllerToCommit animated:NO]; }
在3dtouchpreviewViewController.m中实现:
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor =[UIColor whiteColor]; //你所想要显示的东西 UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)]; textlabel.backgroundColor = [UIColor colorWithRed:54/255.0 green:54/255.0 blue:54/255.0 alpha:1]; textlabel.textColor = [UIColor whiteColor]; textlabel.text = @"哈哈哈哈哈哈"; textlabel.textAlignment = NSTextAlignmentCenter; textlabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:16]; [self.view addSubview:textlabel]; _imageViewShow =[[UIImageView alloc]init]; _imageViewShow.frame = CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height-40); _imageViewShow.image = [UIImage imageNamed:@"picture"]; [self.view addSubview:_imageViewShow]; } //下面的actionview - (NSArray <id <UIPreviewActionItem>> *)previewActionItems { UIPreviewAction *action = [UIPreviewAction actionWithTitle:@"删除" style: UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"实现删除功能!"); }]; return @[action]; }
以上就是3DTouch的简单使用,如有不足之处,欢迎指出。