浅谈3D Touch(2) -- UITouch && Peek && Pop

UITouch

之所以先说UITouch是因为从Peek到Pop这个过程中,相信其内部用到了这个东西,我们来看一下iOS9在这个UITouch中加了哪些东西:

  • UIForceTouchCapability
    • UIForceTouchCapabilityUnknown //3D Touch检测失败
    • UIForceTouchCapabilityUnavailable //3D Touch不可用
    • UIForceTouchCapabilityAvailable //3D Touch可用

这3个枚举值就是我们来判断设备是否开启3D Touch功能,可以在UIViewController生命周期的viewWillAppear中我们是这么判断的:

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
    xxxxxxxx
}

当然在生命周期内,如果用户有意修改了设备的3D Touch功能,我们还有一个地方来重新检测:

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection

刚刚说了Peek和Pop和这个有关,对的,就是这个类中的force属性以及他的最大值maximumPossibleForce,我们在UIView上做一个简单的实验

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    NSLog(@"%f",touch.force);
    self.backgroundColor = [UIColor colorWithRed:(touch.force / touch.maximumPossibleForce )green:0 blue:1 alpha:1];
    
}

按的越重,这个view的red就越大

img_3

img_4

为了得到maximumPossibleForce值,我故意在这个函数中加上了

if (touch.force == touch.maximumPossibleForce)
{
    NSLog(@"%f",touch.force);
}

输出发现当3D touch开的时候这个值为20/3,所以得到结论force是从0~20/3
从Peek到Pop也正是根据这个值大于某个指定的值时触发

Peek & Pop

刚刚说了Peek和Pop的触发和UITouch的force有关,在切入正题之前我们还要将触发这2个事件的另一个东西:

previewActionItems

我们创建一个PeekViewController,在这个利用到了UIPreviewAction 和 UIPreviewActionGroup 2个iOS9新加的类型和他们的初始化方法我们重写

- (NSArray <id <UIPreviewActionItem>> *)previewActionItems

从而来定义previewActionItems,这里没什么难度直接上代码

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    
    // 生成UIPreviewAction
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Action 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 1 selected");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Action 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 2 selected");
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"Action 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Action 3 selected");
    }];
    
    UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"tap 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 1 selected");
    }];
    
    UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"tap 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 2 selected");
    }];
    
    UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"tap 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"tap 3 selected");
    }];
    
    // 塞到UIPreviewActionGroup中
    NSArray *actions = @[action1, action2, action3];
    NSArray *taps = @[tap1, tap2, tap3];
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:actions];
    UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:taps];
    NSArray *group = @[group1,group2];
    
    return group;
}

我们发现他是一个二维结构,所以能展示的东西还是非常多的。

好了准备工作到此结束,我们切入正题
在ViewController中,创建一个label作为触发的view

_label = [[UILabel alloc] initWithFrame:CGRectMake(10, 300, self.view.frame.size.width - 20, 100)];
_label.userInteractionEnabled = YES;
_label.textAlignment = NSTextAlignmentCenter;
_label.backgroundColor = [UIColor yellowColor];
_label.font = [UIFont boldSystemFontOfSize:20];
_label.text = @"Peek && Pop";
[self.view addSubview:_label];

然后检测是否可以使用3DTouch,如果可以就注册:

- (void)check3DTouch
{
    // 如果开启了3D touch
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        [self registerForPreviewingWithDelegate:(id)self sourceView:_label];
        
    }
}

最后我们来写Peek和Pop的代理方法

  • peek:
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //防止重复加入
    if ([self.presentedViewController isKindOfClass:[PeekViewController class]])
    {
        return nil;
    }
    else
    {
        PeekViewController *peekViewController = [[PeekViewController alloc] init];
        return peekViewController;
    }
}

这个代理在按的过程中会进来多次,但是我们并不需要多个peek对象,所以如果触发的self.presentedViewController和我们想要的peekViewController已经是同一个了,那就return nil,我们按label在出现peekViewController的时候向上移动我们能看到刚刚创建的previewActionItems的2个group

img_5

点开一个就能看到我们没个group的items

img_6

点击就会触发我们刚刚在block中定义的事件

  • Pop:
    在Peek的基础上不要向上移动,而是按得更重一些,个人感觉已经到了maximumPossibleForce这个时候触发代理:
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    PopViewController *popViewController = [[PopViewController alloc] init];
    [self showViewController:popViewController sender:self];
}

这时候我们看到了PopViewController

img_7

当然我们在PopViewController还需要给他加个dismiss的方法,让他可以回去

好了,官方说的所有3D Touch的功能都介绍好了,这是我的Demo

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

推荐阅读更多精彩内容

  • 3D Touch简介 2015年,苹果发布了iOS9以及iphone6s/iphone6s Plus,其中最具有创...
    爱恨的潮汐阅读 382评论 0 2
  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 4,463评论 8 18
  • 3D Touch简介 2015年,苹果发布了iOS9以及iphone6s/iphone6s Plus,其中最具有创...
    简简蜗牛阅读 599评论 0 0
  • 前言 对于3DTouch很多人都已经并不陌生了,因为iPhone 6s想必大家都已经体验过了,深度按压带来的体验仁...
    Smallwolf_JS阅读 1,670评论 4 10
  • 3D Touch介绍 从iPhone 6s开始,产品都添加了一项硬件属性,叫做3D touch。作为屏幕的一部分,...
    歪笔书生_阅读 602评论 0 0