3D touch介绍
- 3D touch 是ios9+、iphone6s+的新功能,简单的说3Dtouch就是用力按压,通过3Dtouch增加了一组手势交互方式。3D touch主要常见的使用:
- 1:Home Screen Quick Actions (主屏快捷行为入口)
- 2:peek and pop (预览和弹出)
- 3:Force Properties (按压力度)
- 4: Web view peek and pop API (HTML链接预览功能)
Home Screen Quick Actions
- 在主屏用力按压app图标,可以弹出一个快捷菜单,可以直接进入app的某个功能。文字和图片会因为app在主屏的位置(如左上或右下)决定弹出菜单的位置以及文字在左还是icon在左。
- 有两种方法可以实现Home Screen Quick Actions,一种是使用.plist文件静态声明,另一种是使用 UIApplicationShortcutItem 对象构造。 静态构造的quick actions可以在你的app第一次安装时候就有效果,而动态构造方法必须在第一次运行之后才能有效果,当然,好处就quick actions可以 随时更新和修改。官方文档
UIApplicationShortcutItems
UIApplicationShortcutItemTitle
New Message
UIApplicationShortcutItemSubtitle
open-favorites
UIApplicationShortcutItemIconType
UIApplicationShortcutIconTypeCompose
UIApplicationShortcutItemType
com.mycompany.myapp.newmessage
UIApplicationShortcutItemUserInfo
key2
value2
UIApplicationShortcutItemIconFile
open-favorites
UIApplicationShortcutItemTitle
Favorites
UIApplicationShortcutItemType
com.mycompany.myapp.openfavorites
UIApplicationShortcutItemUserInfo
key1
value1
- (void)add3DTouch {
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"1"];
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"钢铁侠" localizedSubtitle:@"最新大片" icon:icon1 userInfo:@{@"sub":@"最新大片1"}];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"2"];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"黑寡妇" localizedSubtitle:@"最新大片" icon:icon2 userInfo:@{@"sub":@"最新大片2"}];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3"];
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"美国队长" localizedSubtitle:@"最新大片" icon:icon3 userInfo:@{@"sub":@"最新大片3"}];
UIApplicationShortcutIcon *icon4 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"4"];
UIMutableApplicationShortcutItem *item4 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"雷神" localizedSubtitle:@"最新大片" icon:icon4 userInfo:@{@"sub":@"最新大片4"}];
[[UIApplication sharedApplication]setShortcutItems:@[item1,item2,item3,item4]];
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler {
if ([shortcutItem.type isEqualToString:@"playItem"]) {
NSLog(@"点击了playItem");
// FirstViewController *fvc = [[FirstViewController alloc] init];
// [self.window.rootViewController showViewController:fvc sender:nil];
}
if ([shortcutItem.type isEqualToString:@"shareItem"]) {
NSLog(@"点击了shareItem");
// SecondViewController *svc = [[SecondViewController alloc] init];
// [self.window.rootViewController showViewController:svc sender:nil];
}
[_viewController.navigationController popToRootViewControllerAnimated:NO];
_viewController.shortcutName =shortcutItem.localizedTitle;
_viewController.info = shortcutItem.userInfo;
[[NSNotificationCenter defaultCenter] postNotificationName:@"ShortCut" object:nil];
}
- 向tableView中添加3DTouch,需要实现以下方法:
- (nullable UIViewController *)previewingContext:(id )previewingContext viewControllerForLocation:(CGPoint)location {
NSIndexPath * indexPath =[_tableViewList indexPathForRowAtPoint:location];
UITableViewCell * cell = [_tableViewList cellForRowAtIndexPath:indexPath];
if (!cell) {
return nil;
}
DetailViewController *detailVC =[[DetailViewController alloc]initWithTitle:[_arrayData objectAtIndex:indexPath.row]];
detailVC.preferredContentSize = CGSizeMake(0, 0);
previewingContext.sourceRect = cell.frame;
return detailVC;
// UIViewController *vc = [[UIViewController alloc]init];
// vc.view.backgroundColor = [UIColor redColor];
// previewingContext.sourceRect = cell.frame;
// vc.preferredContentSize = CGSizeMake(200, 200);
// return vc;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self.navigationController pushViewController:viewControllerToCommit animated:NO];
}
- (NSArray > *)previewActionItems {
UIPreviewAction *action = [UIPreviewAction actionWithTitle:@"赞" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"点击了赞");
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"吐槽" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"点击了吐槽");
}];
return @[action,action2];
}