3D Touch

来源:互联网 发布:淘宝手机充值软件利润 编辑:程序博客网 时间:2024/05/21 18:49

一,3D Touch有三大模块
《1》Home Screen Quick Actions
通过主屏幕的应用Icon,我们可以用3D Touch呼出一个菜单,进行快速定位应用功能模块相关功能的开发。如上面的日历。
《2》peek and pop
这个功能是一套全新的用户交互机制,在使用3D Touch时,ViewController中会有如下三个交互阶段:
1,提示用户这里有3D Touch的交互,会使交互控件周围模糊
这里写图片描述
2,继续深按,会出现预览视图
这里写图片描述
3,通过视图上的交互控件进行进一步交互
这里写图片描述
这个模块的设计可以在网址连接上进行网页的预览交互。
《3》.Force Properties
iOS9为我们提供了一个新的交互参数:力度。我们可以检测某一交互的力度值,来做相应的交互处理。

二,实现过程
《1》静态标签
首先我们需要在Info.plist文件中加上一项

[key]UIApplicationShortcutItems[/key]  [array]    [dict]    //触发3D Touch时显示的图标类型    [key]UIApplicationShortcutItemIconType[/key]    //显示分享的图标    [string]UIApplicationShortcutIconTypeShare[/string]    //显示名字的    [key]UIApplicationShortcutItemTitle[/key]    [string]Share[/string]    //相当于项目中的Bundle Identifier唯一的,通过这个标示我们可以判断点相应的那一项触发相应的操作    [key]UIApplicationShortcutItemType[/key]    [string]-11.UITouchText.share[/string]    [/dict]  [/array]

如图:
这里写图片描述
UIApplicationShortcutItems:数组中的元素就是我们的那些快捷选项标签。
UIApplicationShortcutItemTitle:标签标题(必填)
UIApplicationShortcutItemType:标签的唯一标识(必填)
UIApplicationShortcutItemIconType:使用系统图标的类型,如搜索、定位、home等(可选)
UIApplicationShortcutItemIconFile:使用项目中的图片作为标签图标(可选)
UIApplicationShortcutItemSubtitle:标签副标题(可选)
UIApplicationShortcutItemUserInfo:字典信息,如传值使用(可选)

《2》appdelete.m中实现以下代码

//创建应用图标上的3D touch快捷选项- (void)creatShortcutItem {    //创建系统风格的icon    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];    //    //创建自定义图标的icon    //    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"分享.png"];    //创建快捷选项    UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.mycompany.myapp.share" localizedTitle:@"分享" localizedSubtitle:@"分享副标题" icon:icon userInfo:nil];    //添加到快捷选项数组    [UIApplication sharedApplication].shortcutItems = @[item];}

效果图
这里写图片描述

//点击快捷选项标签进入应用的响应(appdelegate.m)#pragma mark -- 处理3DTouch触发事件- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{    //判断先前我们设置的唯一标识    if([shortcutItem.type isEqualToString:@"touchShare.bundle"]){        //设置当前的VC 为rootVC        TouchViewController *vc =[[TouchViewController alloc] init];        [self.window.rootViewController presentViewController:vc animated:YES completion:^{        }];    }    else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.search"]){        //进入搜索界面        TouchChildViewController *childVC = [[TouchChildViewController alloc] init];        [self.window.rootViewController presentViewController:childVC animated:YES completion:^{        }];    }    else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.share"])    {//进入分享界面        NSArray *arr = @[@"hello 3D Touch"];        UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil];        [self.window.rootViewController presentViewController:vc animated:YES completion:^{        }];    }    else{        NSLog(@"没有3DTouch的识别符");    }    if (completionHandler) {        completionHandler(YES);    }}

《3》peek(展示预览)和pop(跳页至预览的界面)

//TouchViewController.m-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];    }    cell.textLabel.text = _myArray[indexPath.row];    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {        NSLog(@"3D Touch  可用!");        //给cell注册3DTouch的peek(预览)和pop功能        [self registerForPreviewingWithDelegate:self sourceView:cell];    } else {        NSLog(@"3D Touch 无效");    }    return cell;}

实现协议UIViewControllerPreviewingDelegate

//peek(预览)- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{    //获取按压的cell所在行,[previewingContext sourceView]就是按压的那个视图    NSIndexPath *indexPath = [_myTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];    //设定预览的界面    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    SearchViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"];    childVC.preferredContentSize = CGSizeMake(0.0f,500.0f);    childVC.str = [NSString stringWithFormat:@"我是%@,用力按一下进来",_myArray[indexPath.row]];    //调整不被虚化的范围,按压的那个cell不被虚化(轻轻按压时周边会被虚化,再少用力展示预览,再加力跳页至设定界面)    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,40);    previewingContext.sourceRect = rect;    //返回预览界面    return childVC;}//pop(按用点力进入)- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {     [self showViewController:viewControllerToCommit sender:self]; }

效果图:(当用户按下时cell周边会虚化,增加压力达到一定值会弹出设定的预览界面,继续增加力按压会跳页至预览界面)
这里写图片描述

//弹窗- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {    // setup a list of preview actions    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Aciton1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        NSLog(@"Aciton1");    }];    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Aciton2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        NSLog(@"Aciton2");    }];    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"Aciton3" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        NSLog(@"Aciton3");    }];    NSArray *actions = @[action1,action2,action3];    // and return them (return the array of actions instead to see all items ungrouped)    return actions;}

这里写图片描述
《4》压力

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    NSArray *arrayTouch = [touches allObjects];    UITouch *touch = (UITouch *)[arrayTouch lastObject];    if (touch.view.tag == 105) {        NSLog(@"Began压力 = %f",touch.force);    }}//按住移动or压力值改变时的回调-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    NSArray *arrayTouch = [touches allObjects];    UITouch *touch = (UITouch *)[arrayTouch lastObject];    //通过tag确定按压的是哪个view,注意:如果按压的是label,将label的userInteractionEnabled属性设置为YES    if (touch.view.tag == 105) {        NSLog(@"move压力 = %f",touch.force);    }}
0 0
原创粉丝点击