3D-Touch

//先判断是否支持3D-Touch

[self registPreview];

//图标可多写

UIApplicationShortcutIcon *Icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeUpdate];

UIApplicationShortcutIcon *Icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeBookmark];

//选项

//Type类型

//localizedTitle标题

//localizedSubtitle子标题

//userInfo参数

UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"share" localizedTitle:@"分享" localizedSubtitle:@"这是一个分享选项" icon:Icon1 userInfo:nil];

UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"search" localizedTitle:@"搜索" localizedSubtitle:@"这是一个搜索选项" icon:Icon2 userInfo:nil];

[UIApplication sharedApplication].shortcutItems = @[item1, item2];

}

- (void)registPreview {

//是否支持3D-Touch

//UIForceTouchCapabilityAvailable可以的

if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {

//支持3D-Touch为资源视图注册代理

[self registerForPreviewingWithDelegate:self sourceView:_tableView];

}else{

NSLog(@"不支持3D-Touch");

}

}

#pragma mark --- UIViewControllerPreviewingDelegate

- (nullableUIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)locationNS_AVAILABLE_IOS(9_0) {

//根据位置获取_tableView中的indexPath

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location];

//根据indexPath获取cell

UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];

if(!cell) {

return nil;

}

PreviewViewController *pvc = [[PreviewViewController alloc] init];

pvc.preferredContentSize = CGSizeMake(0, 0);

//被点击的那个小框

previewingContext.sourceRect = cell.frame;

//删除(pvc.h中声明的block块)

pvc.deleteCellBlock = ^(NSIntegerindex) {

[_dataArray removeObjectAtIndex:index];

[_tableView reloadData];

};


//vc.h中声明的index属性

pvc.index = indexPath.row;

return pvc;

}

- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommitNS_AVAILABLE_IOS(9_0) {

//提交视图控制器

[self presentViewController:viewController ToCommitanimated:NO completion:nil];

}


//在PreviewViewController *pvc的.m文件中

//按钮

- (NSArray> *)previewActionItems {

UIPreviewAction *action = [UIPreviewActionaction WithTitle:@"删除" style:UIPreviewActionStyle Destructivehandler:^(UIPreviewAction *_Nonnullaction,UIViewController *_NonnullpreviewViewController) {

_deleteCellBlock(_index);

}];

return @[action];

}

你可能感兴趣的:(3D-Touch)