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:@"嘿嘿"])
{
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];
}
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
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
iOS开发--3D Touch的基本使用的更多相关文章
- iOS开发 - 3D Touch 应用系列一 - Quick Actions 创建桌面 Icon 快捷方式
个言 很久没发随笔了,有一年多了吧.期间也曾想继续去写随笔,但是因为各种原因而耽搁了.最近又想了一下,还是有很多东西想要写,想要分享,想要记录下来的东西.之后我也会不断写随笔,但不止于 iOS 的方向 ...
- 【iOS】3D Touch
文章内容来源于Apple的开发者文档:https://developer.apple.com/library/content/documentation/UserExperience/Conceptu ...
- Android 7.1.1 之实现 3D Touch
转载请注明出处:http://blog.csdn.net/yyh352091626/article/details/68962736 Shortcut概念 详细实现 BuildConfig 配置 静态 ...
- iOS 3D Touch 适配开发
3D Touch的主要应用 文档给出的应用介绍主要有两块: 1.A user can now press your Home screen icon to immediately access fun ...
- 从3D Touch 看 原生快速开发
全新的按压方式苹果继续为我们带来革命性的交互:Peek和Pop,Peek 和 Pop 让你能够预览所有类型的内容,甚至可对内容进行操作,却不必真的打开它们.例如,轻按屏幕,可用 Peek 预览收件箱中 ...
- iOS 3D Touch功能 3 -备
新的触摸体验——iOS9的3D Touch 一.引言 二.在模拟器上学习和测试3D Touch 附.SBShortcutMenuSimulator的安装和使用 三.3D Touch的主要应用 四.3D ...
- iOS 9之3D Touch
金田 北京时间9月10日凌晨, Apple在美国旧金山比尔格拉汉姆公民大礼堂(Bill Graham Civic Auditorium)召开新品发布会.本次着重介绍了3D Touch功能, 大体介绍一 ...
- 3D Touch开发全面教程之Peek and Pop - 预览和弹出
## 3D Touch开发全面教程之Peek and Pop - 预览和弹出 --- ### 了解3D Touch 在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch ...
- 你想知道的3D Touch开发全在这里了
前言 iPhone 6s和iPhone 6s Plus为多点触摸界面带来了强大的3D触摸新维度.这项新技术可以感知用户按下显示屏的深度,让他们比以往任何时候都更能使用你的应用程序和游戏.更多关于3D ...
随机推荐
- 51nod 1109 01组成的N的倍数
用01 组成 N的最小倍数 这个BFS搜索就好. 类似这道: ZOJ Problem Set - 1530 每次 要么是0 要么是1, 记入余数,和前驱. #include<bits/stdc ...
- 多线程 -- NSThread
NSThread NSThread 一个NSThread对象就代表一条线程 创建线程的几种方式 alloc/init // 1.创建线程 NJThread *thread = [[NJThread a ...
- ubuntu cron.hourly不运行
服务器没有装NTP,要每天向特定的server进行时间同步,写了一个定时任务,放在/etc/cron.daily下,但是不运行. /etc/crontab文件: # /etc/crontab: sys ...
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 05.Hibernate多对多关联
前言:本文讲解使用Hibernate映射多对多关联关系,并使用多种方式映射多对多关联. 1.数据库表的多对多关系 本文根据学生信息表(tb_student)和教师信息表(tb_teac ...
- Sprite Kit 入门教程
Sprite Kit 入门教程 Ray Wenderlich on September 30, 2013 Tweet 这篇文章还可以在这里找到 英语, 日语 If you're new here, ...
- On Explainability of Deep Neural Networks
On Explainability of Deep Neural Networks « Learning F# Functional Data Structures and Algorithms is ...
- QGraphics
QGraphicsView和QGraphicsScene QGraphicsScene提供一个场景,可以添加各种item,QGraphicsView用于将元素显示,并支持旋转和缩放:可以将QGraph ...
- codeforces #235div2 D
完全没看出是状态压缩DP, 果然没练习,之前一直再看,看来要把状压做几道了, 上代码吧:代码也是问道的 无语... #include<cstdio> #include<cstring ...
- android 关于Location of the Android SDK has not been setup in the preferences的解决方法
今天在部署android开发环境的时候,每次打开eclipse的时候点击AVD Manager的按钮就会弹出Location of the Android SDK has not been setup ...