iOS开发--3D Touch的基本使用
Posted 专注it
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发--3D Touch的基本使用相关的知识,希望对你有一定的参考价值。
1.桌面快捷菜单项
效果如图:
点击之后的效果如图:
接下来看下具体实现:
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;
}
2).在-application:performActionForShortcutItem:completionHandler:方法中实现点击快捷菜单的方法:
//3D Touch按压程序图标的快捷项时触发的方法
-(void)application:(UIApplication )application performActionForShortcutItem:(UIApplicationShortcutItem )shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
NSString title;
if([shortcutItem.localizedTitle isEqualToString:@"嘿嘿"])
{
[email protected]"嘿嘿";
}
else if([shortcutItem.localizedTitle isEqualToString:@"呵呵"])
{
[email protected]"呵呵";
}
else if([shortcutItem.localizedTitle isEqualToString:@"搜索"])
{
[email protected]"搜索";
}
//这里就弹个框子意思一下
//由于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];
}
2.程序内的3DTouch按压操作:
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;
}
注册3DTouch
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
[email protected][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
以上是关于iOS开发--3D Touch的基本使用的主要内容,如果未能解决你的问题,请参考以下文章
App Shortcuts 快捷方式 Android7.1 的"3D Touch"
Android App 快捷方式之 Android 版本的 3D Touch
如何在 iOS 应用程序的 UIApplicationShortcutIcons(3D touch) 中添加 4 个以上的快速操作项