盡管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的簡單使用,如有不足之處,歡迎指出。