简介:
3D Touch 是苹果推出的在iphone6和6Plus上、iphone7系列的功能。下面就简单介绍一下如何使用在iOS开发中为工程配置这一功能。
实现:
一、重按app 启动图标出现功能选项的实现
实现效果如下:
这一步的操作不需要写代码 只需要配置一下工程中的info.plist 文件就能实现。
下面我配置了3个选项 item0和item1 item2一个是打开选项,一个是朋友圈选项, 一个是聊天。
参数说明:
UIApplicationShortcutItemType:该QuickAction的标示符,在整个工程中,必须唯一,相当于QuickAction的名称,通过该名称,识别用户做了哪个点击;
UIApplicationShortcutItemSubtitle:显示在QuickAction主名称下面的子名称;
UIApplicationShortcutItemIconType:该QuickAction的图标。
可以配置多个QuickAction,如图所示,配置了两个,item0和item1。
其中,系统提供的UIApplicationShortcutItemIconType有如下类型:
UIApplicationShortcutIconTypeCompose,
UIApplicationShortcutIconTypePlay,
UIApplicationShortcutIconTypePause,
UIApplicationShortcutIconTypeAdd,
UIApplicationShortcutIconTypeLocation,
UIApplicationShortcutIconTypeSearch,
UIApplicationShortcutIconTypeShare,
UIApplicationShortcutIconTypeProhibit NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeContact NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeHome NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMarkLocation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeFavorite NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeLove NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCloud NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeInvitation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeConfirmation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMail NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMessage NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeDate NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTime NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCapturePhoto NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCaptureVideo NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTask NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTaskCompleted NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeAlarm NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeBookmark NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeShuffle NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeAudio NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeUpdate NS_ENUM_AVAILABLE_IOS(9_1)
这样配置好plist文件重按启动图标就可以显示出两个选项了。
配置完plist文件,选项也有了,接下来就应该要实现点击选项的功能逻辑实现啦。
二、点击选项的功能实现:
#pragma mark-- 处理点击3D touch 方法的功能实现
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
if ([shortcutItem.type isEqualToString:@"ShortCutOpen"]) {
NSLog(@"打开了软件");
}if ([shortcutItem.type isEqualToString:@"ShortCutShare"]) {
// NSLog(@"朋友圈");tabbarVC
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabbarVC = [story instantiateViewControllerWithIdentifier:@"tabbarVC"];
[tabbarVC setSelectedIndex:1];
[self.window.rootViewController presentViewController:tabbarVC animated:YES completion:nil];
}if ([shortcutItem.type isEqualToString:@"ShortCutChat"]) {
NSLog(@"聊天");
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabbarVC = [story instantiateViewControllerWithIdentifier:@"tabbarVC"];
// DemoVC9 *vc9 = [[DemoVC9 alloc] init];
[tabbarVC setSelectedIndex:2];
[self.window.rootViewController presentViewController:tabbarVC animated:YES completion:nil];
}
}