我认为3D Touch 大概分为两个部分,一个部分是通过主屏幕的应用Icon,直接进入应用的相应功能模块。比如,扫一扫。第二部分,就是用户通过手势在view上来预览一些预加载信息。
我们先说下第一部分:
静态3D touch 可以在info.plist中配置
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemType</key>
<string>com.test.XXX</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Static Shortcut</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>available at launch</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>icon</string>
</dict>
</array>
必填项(下面两个键值是必须设置的):
UIApplicationShortcutItemType 这个键值设置一个快捷通道类型的字符串
UIApplicationShortcutItemTitle 这个键值设置标签的标题
选填项(下面这些键值不是必须设置的):
UIApplicationShortcutItemSubtitle 设置标签的副标题
UIApplicationShortcutItemIconType 设置标签Icon类型
UIApplicationShortcutItemIconFile 设置标签的Icon文件
动态的在Appdelegate中实现
if([UIApplication sharedApplication].shortcutItems.count ==0) {
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"icon1"];
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"ForceTouchIconSign" localizedTitle:@"签到" localizedSubtitle:nil icon:icon1 userInfo:nil];
NSArray *items = @[item1];
NSArray *shortcutItems = [UIApplication sharedApplication].shortcutItems;
NSArray *updateItems = [shortcutItems arrayByAddingObjectsFromArray:items];
[UIApplication sharedApplication].shortcutItems = updateItems;
}
下面方法就是处理手势进入后的方法
– (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler;
预览模式
先遵守代理
@interface ViewController :
UIViewController<UIViewControllerPreviewingDelegate>
@end
然后控制器注册
[self registerForPreviewingWithDelegate:self sourceView:self.view];
最后实现代理
- (nullable UIViewController *) previewingContext: (id<UIViewControllerPreviewing>)previewingContext iewControllerForLocation:(CGPoint)location {
UIViewController *testVC = [[UIViewController alloc] init];
testVC.view.frame = CGRectMake(10, location.y - 10, 300, 300);
testVC.view.backgroundColor = [UIColor redColor];
return testVC;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self showViewController:viewControllerToCommit sender:self];
}
以上就是3D touch的基本方法更多细节请看官方的文档。