3D Touch功能概览
1、Peek and Pop (三部曲)
2、Home Screen Quick Actions(点击应用icon)
3、Force Properties(力度)
一、Home Screen Quick Actions开发
1、静态添加
在info.plist中添加UIApplicationShortcutItems,是数组,数组中添加如下信息
字段名 | 功能 | 备注 |
---|---|---|
UIApplicationShortcutItemType | 唯一标识符 | 必有,可为空 |
UIApplicationShortcutItemTitle | 标题 | 必有,不为空 |
UIApplicationShortcutItemSubtitle | 副标题 | 暂无 |
UIApplicationShortcutItemIconType | icon类型 | 暂无 |
UIApplicationShortcutItemIconFile | 自定义icon | 图标名称 |
UIApplicationShortcutItemUserInfo | 自定义信息 | 字典,传值 |
2、动态添加
// 创建标签的ICON图标。或用iconWithTemplateImageName自定义图标
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
// 创建一个标签,并配置相关属性。
UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"two" localizedTitle:@"么么哒" localizedSubtitle:@"爱你呦" icon:icon userInfo:nil];
// 将标签添加进Application的shortcutItems中。
[UIApplication sharedApplication].shortcutItems = @[item];
// iOS9在AppDelegated中提供了一个新方法,用于处理快速入口启动的App
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
二、Peek and Pop
- 遵循协议
UIViewControllerPreviewingDelegate
,注册协议
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {// 3DTouch可用
// 注册协议
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}else{
NSLog(@"3DTouch不可用");
}
- 返回要跳转的控制器
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
UIViewController *vc = [[UIViewController alloc] init];
// 设置预览时控制器的size
// vc.preferredContentSize = CGSizeMake(200, 200);
CGRect sourceRect = CGRectMake(0, 100, self.tableView.frame.size.width, 50);
// sourceRect就是不被虚化的区域
previewingContext.sourceRect = sourceRect;
return vc;
}
- 实现跳转
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self.navigationController pushViewController:viewControllerToCommit animated:YES]; // 或者present跳转
// [self.navigationController showDetailViewController:viewControllerToCommit sender:self];
}
- 预览时下滑的底部菜单,在预览控制器实现下面代理即可
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems {
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Aciton1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton1");
}];
return @[action1];
}
网友评论