3DTouch - iOS新特性

6s和6s plus之后特有效果,对着应用图标用力按会触发3DTouch .


第一步 : 3DTouch 设备支持检测:

检测当前的设备是否支持3DTouch

//  在iOS9中有一个新的枚举
 typedef NS_ENUM(NSInteger, UIForceTouchCapability) {  
            UIForceTouchCapabilityUnknown        = 0,  // 未知的支持属性
            UIForceTouchCapabilityUnavailable    = 1,  // 不支持
            UIForceTouchCapabilityAvailable      = 2 // 支持
  };

一般我们都在每个ViewController的生命周期中这样做:

定义一个是否设备支持的BOOL值属性

 @property (nonatomic , assign) BOOL support3DTouch; 

在生命周期函数中检测支持与否

  - (void)viewWillAppear:(BOOL)animated {  
     [super viewWillAppear:animated];  
      //检测当前是否支持3DTouch  
      self.support3DTouch = [self support3DTouch];  
  }

在生命周期外检测支持与否(因为有可能出了生命周期函数而发生了变化)

  - (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection NS_AVAILABLE_IOS(8_0) {     
      self.support3DTouch = [self support3DTouch];  
  } 

检测是否支持3DTouch的方法

  - (BOOL)support3DTouch  
  {  
      // 如果开启了3D touch  
      if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)  
      {  
          return YES;  
      }  
          return NO;  
      }  
  }  

第二步 : 配置快捷视图列表

创建快捷视图列表有两种方法:
1,一种是编辑info.plist文件中的UIApplicationShortcutItems,
通过可视化的界面添加键值对直接配置info.plist

2,另一种是使用代码在工程中加入items
在工程的 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
    [self.window makeKeyAndVisible];
    
    //  代码创建快捷视图列表的方法,
     [self create3DTouchShotItems];

    return YES;
}

- (void)create3DTouchShotItems {
    //创建快捷item的icon UIApplicationShortcutItemIconFile
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon2"];
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon3"];
    
    //创建快捷item的userinfo UIApplicationShortcutItemUserInfo
    NSDictionary *info1 = @{@"url":@"url1"};
    NSDictionary *info2 = @{@"url":@"url2"};
    NSDictionary *info3 = @{@"url":@"url3"};
    
    //创建ShortcutItem
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_1" localizedTitle:@"扫一扫" localizedSubtitle:@"" icon:icon1 userInfo:info1];
    UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_2" localizedTitle:@"smile" localizedSubtitle:@"微笑面对生活" icon:icon2 userInfo:info2];
    UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"XS_3DTocuh_3" localizedTitle:@"购物" localizedSubtitle:@"Shopping" icon:icon3 userInfo:info3];
    
    NSArray *items = @[item1, item2, item3];
    [UIApplication sharedApplication].shortcutItems = items;
}

第三步 : 给列表视图中的cell注册 3DTouch 事件

1,首先,在首页当前控制器里遵守UIViewControllerPreviewingDelegate协议
UIViewControllerPreviewingDelegate
2,在注册前先判断是否设备支持(也就是第一步)

3,注册: [self registerForPreviewingWithDelegate:self sourceView:cell];

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLTableViewCell"];
    if (cell == nil) {
        cell = [ZLTableViewCell cellWithTableView:tableView];
    }
    cell.dataFrame = self.dataSource[indexPath.row];
    //给cell注册代理,使其支持3DTouch手势
    if (self.support3DTouch) {
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    }
    
    return cell;
}

第四步: 完成UIViewControllerPreviewingDelegate 协议回调,实现Peek Pop

在首页当前控制器里,

#pragma mark - 3DTouch  UIViewControllerPreviewingDelegate

Peek 实现代码:

// 此方法是轻按控件时,跳出peek的代理方法
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
    //防止重复加入
    if ([self.presentedViewController isKindOfClass:[ZLPeekViewController class]])
    {
        return nil;
    }
    else
    {
        ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
        ZLCellData * cellData = cell.dataFrame.cellData;
        ZLPeekViewController *peekViewController = [[ZLPeekViewController alloc] init];
        peekViewController.cellData = cellData;
        peekViewController.delegate = self;
        return peekViewController;
    }
}

Pop 代码

//此方法是重按peek时,跳入pop的代理方法
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
    commitViewController:(UIViewController *)viewControllerToCommit {
    
    ZLTableViewCell *cell = (ZLTableViewCell *)previewingContext.sourceView;
    ZLCellData * cellData = cell.dataFrame.cellData;
    ZLPopViewController *popViewController = [[ZLPopViewController alloc] init];
    popViewController.cellData = cellData;
    // 以prentViewController的形式展现
    [self showViewController:popViewController sender:self];
    
    // 以push的形势展现
//    [self.navigationController pushViewController:popViewController animated:YES];
}

第五步 : 在Peek状态下向上滑动出现的按钮配置方法

在 ZLPeekViewController.m 里, 实现 - (NSArray> *)previewActionItems 回调方法

#pragma mark - Preview Actions

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    
    // 生成UIPreviewAction
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"事件 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 1 selected");
        [self.delegate pushToPopViewControllerWithCellData:self.cellData];
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"事件 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 2 selected");
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"事件 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 3 selected");
    }];
    
    UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"按钮 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 1 selected");
    }];
    
    UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"按钮 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 2 selected");
    }];
    
    UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"按钮 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 3 selected");
    }];
    
    NSArray *actions = @[action1, action2, action3];
    NSArray *taps = @[tap1, tap2, tap3];
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"一组事件" style:UIPreviewActionStyleDefault actions:actions];
    UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"一组按钮" style:UIPreviewActionStyleDefault actions:taps];
    NSArray *group = @[group1,group2];
    
    //当然你也可以返回三个单独的action对象的数组,而不是group,具体效果,可以自己试一下
    
    return group;
}

现在可以测试喽, 看下效果吧, 如果需要demo可以去我的 GitHub 上下载~

3DTouch.gif

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

推荐阅读更多精彩内容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 22,930评论 8 183
  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 4,377评论 8 18
  • 专注路程,继续摘抄: 雪夜林畔小驻 想来我认识这座森林, 林主的庄宅就在邻村, 却不会见我在此驻马, 看他林中积雪...
    舒斗斗阅读 90评论 0 0
  • 北风西南度,狂雨扫申城 雁飞叶纷落,风沁骨髓寒 老有毡帽戴,少且綿作衣 古只有朝丝,行刁几人还?
    秋风起嘻云飞扬阅读 322评论 0 0
  • 连自己都不知道该如何去对你 冷风千随 月下成影 初时便觉 言之时 寒冬秋雨 至如今 深思切 便让春风伴你 只言句我来迟了
    正捌阅读 250评论 7 10