标签:
效果如图:
点击之后的效果如图:
接下来看下具体实现:
1).在-application:didFinishLaunchingWithOptions:
方法中用-setShortcutItems:方法来添加快捷菜单项。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//3D Touch按压程序图标的快捷项
//快捷菜单的图标
UIApplicationShortcutIcon *icon1=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCaptureVideo];
UIApplicationShortcutIcon *icon2=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIApplicationShortcutIcon *icon3=[UIApplicationShortcutIcon iconWithTemplateImageName:@"search"];
//快捷菜单
UIApplicationShortcutItem *item1=[[UIApplicationShortcutItem alloc]initWithType:@"1"
localizedTitle:@"嘿嘿"
localizedSubtitle:nil
icon:icon1
userInfo:nil];
UIApplicationShortcutItem *item2=[[UIApplicationShortcutItem alloc]initWithType:@"1"
localizedTitle:@"呵呵"
localizedSubtitle:@"干嘛去洗澡"
icon:icon2
userInfo:nil];
UIApplicationShortcutItem *item3=[[UIApplicationShortcutItem alloc]initWithType:@"1"
localizedTitle:@"搜索"
localizedSubtitle:nil
icon:icon3
userInfo:nil];
//设置app的快捷菜单
[[UIApplication sharedApplication] setShortcutItems:@[item1,item2,item3]];
//导航
self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[ViewController new]];
return YES;
}
//3D Touch按压程序图标的快捷项时触发的方法
-(void)application:(UIApplication )application performActionForShortcutItem:(UIApplicationShortcutItem )shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
NSString title;
if([shortcutItem.localizedTitle isEqualToString:@"嘿嘿"])
{
title=@"嘿嘿";
}
else if([shortcutItem.localizedTitle isEqualToString:@"呵呵"])
{
title=@"呵呵";
}
else if([shortcutItem.localizedTitle isEqualToString:@"搜索"])
{
title=@"搜索";
}
//这里就弹个框子意思一下
//由于UIAlertView在iOS 9被废弃,因此选用UIAlertController
UIAlertController alertController=[UIAlertController alertControllerWithTitle:@"提示"
message:[NSString stringWithFormat:@"你点击了“%@”",title]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction action=[UIAlertAction actionWithTitle:@"知道了"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:action];
[self.window.rootViewController presentViewController:alertController
animated:YES
completion:nil];
}
1).首先,实现3DTouch的视图控制器要遵守:
UIViewControllerPreviewingDelegate协议,它有2个required级别的协议方法:
-previewingContext:viewControllerForLocation:
以及
-previewingContext:commitViewController:
2).检测3DTouch是否可用,并注册3DTouch:
检测3D Touch是否可用
-(BOOL)is3DTouchAvailiable
{
if(self.traitCollection.forceTouchCapability==UIForceTouchCapabilityAvailable)
return YES;
return NO;
}
if([self is3DTouchAvailiable])
{
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
3).实现协议方法:
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath *indexPath=[_tbVew indexPathForRowAtPoint:CGPointMake(location.x, location.y-64)];
if(indexPath)
{
DetailViewController *detail=[[DetailViewController alloc]init];
detail.title=_dataArray[indexPath.row];
//detail.preferredContentSize=CGSizeMake(300, 300);
__weak typeof(self) wkSelf=self;
//------------上拉时的菜单-------------------
//置顶及其点击逻辑
UIPreviewAction *topAction=[UIPreviewAction actionWithTitle:@"置顶" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * action, UIViewController * previewViewController) {
[wkSelf.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
[wkSelf.tbVew reloadData];
[wkSelf showAlert:@"提示" body:@"已置顶"];
}];
//删除及其点击逻辑
UIPreviewAction *deleteAction=[UIPreviewAction actionWithTitle:@"删除" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction *action, UIViewController * previewViewController) {
[wkSelf.dataArray removeObjectAtIndex:indexPath.row];
[wkSelf.tbVew reloadData];
[wkSelf showAlert:@"警告" body:@"已删除"];
}];
//传递上拉菜单项给detail
detail.actions=@[topAction,deleteAction];
return detail;
}
return nil;
}
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
4).要previewing的视图控制器必须实现:
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems方法,该方法定义了peek时上拉出来的菜单:
/**peek时上拉出来的菜单*/
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
return self.actions;
}
https://github.com/whj111/3D_Touch_Demo
标签:
原文地址:http://www.cnblogs.com/wanghuaijun/p/5314831.html