3D Touch
// 创建标签的ICON图标。
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
// 创建一个标签,并配置相关属性。
UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"two" localizedTitle:@"么么哒" localizedSubtitle:@"爱你呦" icon:icon userInfo:nil];
// 将标签添加进Application的shortcutItems中。
[UIApplication sharedApplication].shortcutItems = @[item , item];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"cell"];
cell.textLabel.text = [NSString stringWithFormat:@"%@" , _data[indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//判断是否支持3d touch
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
//注册3d touch 的代理 和预览的view
[self registerForPreviewingWithDelegate:self sourceView:cell];
}
return cell;
}
//遵循代理, 实现代理方法
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0)
{
NSIndexPath * index = [_tableView indexPathForCell:(UITableViewCell*)[previewingContext sourceView]];
RedViewController *bgView = [[RedViewController alloc]init];
bgView.view.backgroundColor = [UIColor redColor];
//设置预览的控制器视图的大小
bgView.preferredContentSize = CGSizeMake(0.0f, 500.0f);
//设置预览view 的模糊范围
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,44);
previewingContext.sourceRect = rect;
return bgView;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0);
{
//重按弹出的控制器
[self showViewController:viewControllerToCommit sender:self];
}
//修改重置item
id mutableItem = [UIApplication sharedApplication].shortcutItems;
//获取第0个shortcutItem
id oldItem = [mutableItem objectAtIndex: 0];
//将旧的shortcutItem改变为可修改类型shortcutItem
id item = [oldItem mutableCopy];
//修改shortcutItem的显示标题
[item setLocalizedTitle: @"Click Lewis"];
[item setLocalizedSubtitle:@"xiixixixi"];
[item setIcon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"saoma"]];
[mutableItem replaceObjectAtIndex:0 withObject:item];
[mutableItem removeLastObject];
[[UIApplication sharedApplication] setShortcutItems:mutableItem];
//在重按弹出的控制器中写这个方法
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
// setup a list of preview actions
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"吃翔好厉害的样子" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton1");
if (self.block) {
self.block();
}
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"吃翔棒棒哒" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton2");
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"吃翔你咋不上天呢" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton3");
}];
UIPreviewAction *action4 = [UIPreviewAction actionWithTitle:@"吃翔你咋不上天呢" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton3");
}];
UIPreviewAction *action5 = [UIPreviewAction actionWithTitle:@"吃翔你咋不上天呢" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton3");
}];
NSArray *actions = @[action1,action2,action3,action4,action5];
// and return them (return the array of actions instead to see all items ungrouped)
return actions;
}
效果如下图:(action1 ,2 ,3 。。。。)