iOS9 3DTouch学习

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

Home Screen Quick Actions(主屏幕捷径)

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


iOS9 3DTouch学习_第1张图片
Home Screen Quick Actions

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

第一种:static quick actions

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

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

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

iOS9 3DTouch学习_第3张图片
静态设置

运行图:

iOS9 3DTouch学习_第4张图片
静态

显示的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图标得到如图:

iOS9 3DTouch学习_第5张图片
动态后

可以发现,虽然我动态设置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。

iOS9 3DTouch学习_第6张图片
preview_available

Peek

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


iOS9 3DTouch学习_第7张图片
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)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 )previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    //这个就是Peek后再用力按就干什么的方法了,下面代码就相当于push了
    [self showViewController:viewControllerToCommit sender:self];
}
Peek quick actions

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

- (NSArray> *)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];
}
iOS9 3DTouch学习_第8张图片
Peek quick actions
Web View Peek and Pop

原理和上面差不多

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

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

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

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

你可能感兴趣的:(iOS9 3DTouch学习)