iOS 3D Touch 开发

1 3D Touch 的三大模块

在我们的app中使用3D Touch功能,主要分一下三个模块:

1.1 Home Screen Quick Actions

通过主屏幕的应用Icon,我们可以用3D Touch 呼出一个菜单,进行快速定位应用功能模块相关功能的开发。

maps_directions_home_2x.png

1.2 Peek and Pop

这个功能是一套全新的用户交互机制,在使用 3D Touch时, 会有如下三个交互阶段:
(1) 轻按,提示用户这里有3D Touch 的交互,会使交互空间周围模糊

preview_available_2_2x.png

(2) 继续深按,会出现预览视图

peek_2x.png

(3) 向上滑动预览视图通过视图上的交互控件进行进一步交互

peek_quick_actions_2x.png
 这个模块的设计可以在网址链接上进行网页的预览交互。

1.3 Force Properties

iOS9 为我们提供了新的交互参数:force 和 maximumPossibleForce 。

2 Home Screen Quick Actions 功能

应用最多有4个快捷选项标签,有两种方式开发 Home Screen Quick Actions 功能。

2.1 静态标签
打开项目的plist文件,添加如下项

静态标签.png

选项介绍
UIApplicationShortcutItems : 数组中的元素就是我们的那些快捷选项标签.
UIApplicationShortcutItemType : 标签的唯一 标示(必填)
UIApplicationShortcutItemTitle : 标签的标题(必填)
UIApplicationShortcutItemSubtitle : 标签的副标题 (选填)
UIApplicationShortcutItemIconType : 标签的Icon类型 (选填)
UIApplicationShortcutItemIconFile : 标签的Icon 文件,图标格式35x35像素单色 (选填)
UIApplicationShortcutItemUserInfo: 字典信息,传值使用(选填)

2.2 动态标签

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] init];
    
    ViewController *mainView = [[ViewController alloc] init];
    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView];
    self.window.rootViewController = mainNav;
    [self.window makeKeyAndVisible];
    
    //动态创建应用图标上的3D touch快捷选项
    [self creatShortcutItem];
    
    UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
    //如果是从快捷选项标签启动app,则根据不同标识执行不同操作,然后返回NO,防止调用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
    if (shortcutItem) {
        //判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作
        if([shortcutItem.type isEqualToString:@"com.dimi.diandi86.main"]){
            
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.home"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.wonderful"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        }
        return NO;
    }
    
    return YES;
}

- (void)creatShortcutItem
{
    //创建系统风格的icon
    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    
    //    //创建自定义图标的icon,图标格式35x35像素单色
    //UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Wonderful.png"];
    
    //创建快捷选项
    UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc] initWithType:@"com.dimi.diandi86.wonderful" localizedTitle:@"精彩" localizedSubtitle:nil icon:icon userInfo:nil];
    
    //添加到快捷选项数组
    [UIApplication sharedApplication].shortcutItems = @[item];
}

效果图如下:


3D Touch 快捷标签.png

2.3 点击快捷选项标签进入应用的响应

//如果app在后台,通过快捷选项标签进入app,则调用该方法,如果app不在后台已杀死,则处理通过快捷选项标签进入app的逻辑在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
    //判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作
    if (shortcutItem) {
        //判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作
        if([shortcutItem.type isEqualToString:@"com.dimi.diandi86.main"]){
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.home"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.wonderful"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        }
    }
    
    if (completionHandler) {
        completionHandler(YES);
    }
}

效果图如下


点击快捷标签选项的响应.png

3 Peek and Pop 功能

应用启动之后界面如下
3D Touch主界面.png

3.1 让ViewController 遵守UIViewControllerPreviewingDelegate协议

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UIViewControllerPreviewingDelegate>

@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

3.2 给 cell 注册 3D Touch 的 Peek and Pop 功能

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cellIdentifier";
    UITableViewCell * cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.textLabel.text = [self.dataArray objectAtIndex: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;
}

3.3 实现 UIViewControllerPreviewingDelegate 方法

#pragma mark - UIViewControllerPreviewingDelegate method
//peek(预览模式)
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //获取按压的cell所在行,[previewingContext sourceView]就是按压的那个视图
    NSIndexPath *indexPath = [self.mainTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
    
    //设定预览的界面
    DimiViewController *childVC = [[DimiViewController alloc] init];
    childVC.preferredContentSize = CGSizeMake(0.0f,500.0f);
    
    //调整不被虚化的范围,按压的那个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
{
    DimiViewController *childVC = [[DimiViewController alloc] init];
    
    [self.navigationController pushViewController:childVC animated:YES];
}

预览界面效果图如下

预览界面效果图.png

3.4 预览界面向上滑动交互实现,在DimiViewController 中实现previewActionItems方法

- (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");
    }];
    
    NSArray *actions = @[action1,action2];
    
    return actions;
}

效果图如下

预览交互效果图.png

4 Force Properties 的运用

-(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 == 520) {
        self.touchTipValueLabel.text = [NSString stringWithFormat:@"压力值:%f",touch.force];
    }
}

#pragma mark - getter and setter
- (UILabel*)touchLabel
{
    if (_touchLabel == nil) {
        _touchLabel = [[UILabel alloc] init];
        _touchLabel.font = [UIFont systemFontOfSize:25];
        _touchLabel.text = @"按下我";
        _touchLabel.userInteractionEnabled = YES;
        _touchLabel.textAlignment = NSTextAlignmentCenter;
        _touchLabel.tag = 520;
    }
    
    return _touchLabel;
}

- (UILabel*)touchTipValueLabel
{
    if (_touchTipValueLabel == nil) {
        _touchTipValueLabel = [[UILabel alloc] init];
        _touchTipValueLabel.font = [UIFont systemFontOfSize:25];
        _touchTipValueLabel.text = @"压力值:";
        _touchTipValueLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _touchTipValueLabel;
}

效果图如下

压力值的运用.png

3D Touch 学习就到此结束啦...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,176评论 5 469
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,190评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,232评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,953评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,879评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,177评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,626评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,295评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,436评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,365评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,414评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,096评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,685评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,771评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,987评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,438评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,032评论 2 341

推荐阅读更多精彩内容