博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS扩展新特性之3DTouch开发
阅读量:6857 次
发布时间:2019-06-26

本文共 3687 字,大约阅读时间需要 12 分钟。

3D Touch是在iPhone6s之后且系统是iOS9以上才能使用的功能,详情见官方文档

3D Touch总的来说分如下两种 (1)A user can now press your Home screen icon to immediately access functionality provided by your app. (2)Within your app, a user can now press views to see previews of additional content and gain accelerated access to features. 一种是按压应用icon弹出的快捷菜单,另一种是在应用里面,按压view弹出另一个视图,再深按一次可push到另一个页面。

一、Home Screen Quick Actions(按压应用图标) 在info.plist文件里添加一项UIApplicationShortcutItems,这是个数组,里面添加任意项,Item0里面的三项分别是图片名称、文本内容、类型,在AppDelegate里是根据这个类型跳转到指定页的。

在AppDelegate如下方法里实现跳转 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

if (articleType == kTypeAttetion) {    [self.tabbar setTabBarSelectedWithIndex:kTabbarTypeAttention];} else {    [self.tabbar setTabBarSelectedWithIndex:kTabbarTypeHome];}HBBaseNavController *nc = (HBBaseNavController *)self.tabbar.selectedViewController;[nc popToRootViewControllerAnimated:YES];UIViewController *vc = [[UIViewController alloc] init];  [nc pushViewController:vc animated:YES];复制代码

跳转的时候要注意的是,先让TabBarViewController跳转到指定Tab,再让这个Tab下的NavigationController 回到根视图(popToRootViewController),然后再push到对应的ViewController里去。

二、Peek and Pop(弹出一个视图和push到一个新的页面)

1.注册事件 如果是在一个tableview上实现这个功能,则需要在cellForRow里面去注册这个事件,

if (IOS9_OR_LATER) {    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {        if (!cell.hasRegister3DTouch) {            [[self viewController] registerForPreviewingWithDelegate:HBTouchDelegate sourceView:cell];            cell.hasRegister3DTouch = YES;        }    }}复制代码

这里面有两点重要的处理:

*第一点是关于这个cell是否被注册过,由于tableview的cell是复用的,所以只注册一遍即可,否则在列表上拉加载几屏后会出现严重的卡顿。

*第二点是这个View所属ViewController的代理指向问题,如果有多个列表,每个列表里都要注册这个代理,这样每个列表里都要实现这个代理的方法,同样的方法要写很多遍,这明显不符合我们编程简洁、高效的宗旨,所以可以把代理的实现抽象成一个类,然后注册时将代理指向这个类(在上面代码中指的是HBTouchDelegate,实现的就是UIViewControllerPreviewingDelegate的两个代理方法)。

2.实现代理方法 #pragma mark - UIViewControllerPreviewingDelegate - (nullable UIViewController *)previewingContext:(id )previewingContext viewControllerForLocation:(CGPoint)location {

UIView *sourceCell = [previewingContext sourceView];if (![[sourceCell viewController].presentedViewController isKindOfClass:[HBPeekViewController class]]) {    HBPeekViewController *peekViewController = [HBPeekViewController new]; peekViewController.preferredContentSize = CGSizeMake(0, 300);//这个是弹出视图的宽高,默认是屏幕宽高    return peekViewController; } else {    return nil;}}复制代码

- (void)previewingContext:(id 
)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0) {[[[previewingContext sourceView] viewController] jumpWithArticleEntity:(HBArticleEntity *)self.touchEntity];}复制代码

3.peekViewController中给弹出的视图添加一些快捷操作,点赞、收藏、对上面的文字进行复制等等,只要在previewActionItems方法,创建UIPreviewAction到数组中,可以在UIPreviewAction的block里实现对应的操作即可

- (NSArray
>*)previewActionItems {// 生成UIPreviewAction UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"赞" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { //do something }];UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"看全文" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { //do something}];UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { //do something }]; NSArray *group = nil; group = @[action1, action2, action3]; return group;}复制代码

到这里3DTouch主要的几点就讲完啦!

转载地址:http://vmiyl.baihongyu.com/

你可能感兴趣的文章
「小程序JAVA实战」小程序视频封面处理(48)
查看>>
8.ES6测试
查看>>
Spring整合JUnit4测试使用注解引入多个配置文件
查看>>
转 原生js中的this指向四条定律
查看>>
centos7 开启防火墙端口 firewalld
查看>>
day25 Python
查看>>
linux影响上传文件大小的因素
查看>>
密码爆破脚本
查看>>
bzoj4788: [CERC2016]Bipartite Blanket
查看>>
Html5的一些基础知识
查看>>
java 创建线程
查看>>
Python全栈开发day3
查看>>
关于onSaveInstanceState的javadoc的渣渣翻译
查看>>
菜鸡的2017CPPC网络赛
查看>>
ADO.NET中的5个对象
查看>>
php的数据访问和封装运用
查看>>
C3----几个常用的加载图标制作
查看>>
python_面向对象小试题
查看>>
Windows、Linux系统安装JDK配置Java环境变量
查看>>
JSON工具类
查看>>