美文网首页
iOS9 3DTouch学习

iOS9 3DTouch学习

作者: loongod | 来源:发表于2017-03-01 22:34 被阅读66次

3D Touch是iOS9以后才能使用的相关功能。

Home Screen Quick Actions(主屏幕捷径)

3D Touch第一个用法就是在主屏幕的时候,通过按压app的图标显示出一些快捷操作


Home Screen Quick Actions

有2种方法可以设置Home Screen Quick Actions

第一种:static quick actions

通过在Info.plist文件中设置UIApplicationShortcutItems数组,如图:

UIApplicationShortcutItems_plist
名字 解释
UIApplicationShortcutItemType (必须) 用来标识应用响应用户action的唯一标识
UIApplicationShortcutItemTitle (必须) 快捷操作的title
UIApplicationShortcutItemSubtitle 快捷操作的subTitle
UIApplicationShortcutItemIconType 系统的快捷图标
UIApplicationShortcutItemIconFile 自定义的图标
UIApplicationShortcutItemUserInfo app定义的一些参数

下面手动在Info.plist中写死的static quick actions

静态设置

运行图:

静态

显示的quick actions列表中,有图标和文字,图标在左边或者在右边是系统自动适配的。

第二种:dynamic quick actions

通过运行app后,执行相关的代码才能显示出来的quick actions

//设置动态的quick actions
- (void)configDynamicQuickActions {

    UIApplicationShortcutIcon *tempItem1Icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome];
    
    UIApplicationShortcutItem *tempItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"com.resume.third" localizedTitle:@"我是动态的1" localizedSubtitle:@"我是运行程序后才出现的" icon:tempItem1Icon userInfo:nil];
    
    UIApplicationShortcutItem *tempItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"com.resume.fourth" localizedTitle:@"我是动态的2"];
    [UIApplication sharedApplication].shortcutItems = @[tempItem1,tempItem2];
}

然后启动app,返回主屏幕,按压app图标得到如图:

动态后

可以发现,虽然我动态设置2个,加上静态设置的3个,但是并没有出现5个,所以quick actions是最多显示4个。

点击后,app中响应的方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler {
    //根据设置的type唯一标识,来做具体的事情
    if ([shortcutItem.type isEqualToString:@"com.demoCollection.first"]) {
        [self showShortcutAlertWithTitle:shortcutItem.localizedTitle message:shortcutItem.localizedSubtitle];
    }else if ([shortcutItem.type isEqualToString:@"com.demoCollection.second"]){
        [self showShortcutAlertWithTitle:shortcutItem.localizedTitle message:[NSString stringWithFormat:@"subtitle:%@\nuserinfo:%@",shortcutItem.localizedSubtitle,[shortcutItem.userInfo objectForKey:@"keyName"]]];
    }
    completionHandler(YES);
}
Peek and Pop
Indication of peek availability

轻按某个地方,如果周围出现虚化模糊的情况,就代表这个Item可以peek(预览、偷窥)
previewingContext.sourceRect这属性来设置源地方,就是轻按不虚化的rect。

preview_available
Peek

稍微用力按Cell会出现如图(图片压缩太厉害了,字有点看不清了)


Peek

设置代理

    //设置代理,forceTouchCapability是判断该设备是否支持3DTouch
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }else {
        UIAlertController *tipsAlertVC = [UIAlertController alertControllerWithTitle:@"3DTouch提示" message:@"该设备不支持3DTouch" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [tipsAlertVC addAction:okAction];
        
        [self presentViewController:tipsAlertVC animated:YES completion:nil];
    }

实现代理方法

#pragma mark UIViewControllerPreviewingDelegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
    //location找到对应数据源
    CGPoint convertPoint = [self.view convertPoint:location toView:self.tableView];
    NSIndexPath *clickIndexPath = [self.tableView indexPathForRowAtPoint:convertPoint];
    
    //设置Peek的sourceRect,sourceRect就是Peek动画弹出的位置,轻按的时候周围会虚化
    UITableViewCell *clickCell = [self.tableView cellForRowAtIndexPath:clickIndexPath];
    //sourceRect对应的是self.view坐标系中的rect,所以需要转换一下
    CGRect convertRect = [self.view convertRect:clickCell.frame fromView:self.tableView];
    previewingContext.sourceRect = convertRect;
    
    TouchDetailVC *detailVC = [[TouchDetailVC alloc] init];
    detailVC.touchModel = [_listViewModel.touchListDataArray objectAtIndex:clickIndexPath.row];
    //设置宽度没用,只设置高度就行了
    detailVC.preferredContentSize = CGSizeMake(0, 400);
    return detailVC;
}

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    //这个就是Peek后再用力按就干什么的方法了,下面代码就相当于push了
    [self showViewController:viewControllerToCommit sender:self];
}
Peek quick actions

在peek时,有个往上拉的箭头,这个时候往上拉,会出现一些选项。这些选项是需要在你peek的UIViewController里面实现下面的方法的

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    
    UIPreviewAction *firstItem = [UIPreviewAction actionWithTitle:@"确定" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"%@",action.title);
    }];
    
    UIPreviewAction *secondItem = [UIPreviewAction actionWithTitle:@"喜欢" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"%@",action.title);
    }];
    
    UIPreviewAction *thirdItem = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"%@",action.title);
    }];
    
    return @[firstItem, secondItem, thirdItem];
}
Peek quick actions
Web View Peek and Pop

原理和上面差不多

#pragma mark UIViewControllerPreviewingDelegate
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
    previewingContext.sourceRect = previewingContext.sourceView.frame;
    
    return [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"http:www.jianshu.com"]];
}

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    [self showViewController:viewControllerToCommit sender:self];
}
Force Properties

在iOS9中,UITouch中增加了2个属性:forcemaximumPossibleForce
force :压力,
maximumPossibleForce:最大压力。

详细使用方法,请看代码:github

相关文章

  • 3DTouch 使用

    iOS9之后使用3DTouch 3DTouch功能主要分为两大块:主屏幕Icon上的Quick Action;Pe...

  • 详解3DTouch的使用

    3DTouch的简单使用. 介绍 3DTouch是iOS9的时候出来,对硬件也有要求,也就是说只有iPhone6s...

  • iOS9 3DTouch学习

    3D Touch是iOS9以后才能使用的相关功能。 Home Screen Quick Actions(主屏幕捷径...

  • [iOS]iOS9 3DTouch、ShortcutItem、P

    3DTouch UITouch类里API的变化 iOS9中添加的属性 altitudeAngle 当笔平行于平面时...

  • Swift开发之3DTouch实用演练

    Swift开发之3DTouch实用演练 2015年,苹果发布了iOS9以及iphone6s/iphone6s Pl...

  • iOS9 3DTouch

    在info.plist中添加如下代码: 解释: UIApplicationShortcutItemUserInfo...

  • 3D Touch功能

    一、功能介绍: 开发环境及调试设备:Xcode7或以上,iOS9或以上,iPhone6s或以上3DTouch功能主...

  • iOS开发3DTouch功能的实现

    开发环境及调试设备支持: Xcode7或以上,iOS9或以上,iPhone6s或以上 3DTouch功能主要分为两...

  • iOS开发 实现3DTouch

    1.添加3DTouch (我是自己封装的 只是在app 启动的时候吊起) 1.1判断是不是可以使用(ios9 之前...

  • iOS APP开发添加3D Touch

    本文就iOS开发中如何集成3DTouch做下简单的讲解。 开发环境及调试设备:Xcode7或以上,iOS9或以上,...

网友评论

      本文标题:iOS9 3DTouch学习

      本文链接:https://www.haomeiwen.com/subject/brjqgttx.html