3D Touch 是苹果iOS9推出的新特性,同样是iPhone6s和6sPlus的一个新功能。之前的苹果设备则不能实现这样的功能。它的出现使app更加便捷化,能够让用户快速的定位到目的页面。
实现3D Touch功能有 动态 和 静态 两种:
动态:
//标题一
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItemalloc]initWithType:@"TianYou.Home"localizedTitle:@"首页"localizedSubtitle:@"副标题一"icon:[UIApplicationShortcutIconiconWithType:UIApplicationShortcutIconTypeHome]userInfo:nil];
//标题二
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItemalloc]initWithType:@"TianYou.Found"localizedTitle:@"发现"localizedSubtitle:@"副标题二"icon:[UIApplicationShortcutIconiconWithType:UIApplicationShortcutIconTypeBookmark]userInfo:nil];
//标题三
UIApplicationShortcutItem *item3 = [[UIApplicationShortcutItemalloc]initWithType:@"TianYou.Activity"localizedTitle:@"活动"localizedSubtitle:@"副标题三"icon:[UIApplicationShortcutIconiconWithType:UIApplicationShortcutIconTypeConfirmation]userInfo:nil];
//标题四
UIApplicationShortcutItem *item4 = [[UIApplicationShortcutItemalloc]initWithType:@"TianYou.My"localizedTitle:@"我的"localizedSubtitle:@"副标题四"icon:[UIApplicationShortcutIconiconWithType:UIApplicationShortcutIconTypeLove]userInfo:nil];
// shortcutItems是一个数组
[UIApplicationsharedApplication].shortcutItems =@[item1,item2,item3,item4];
静态:
然后加上判断就能快捷启动了如下:
// iOS9 的 3D Touch 判断
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
if ([[[UIDevicecurrentDevice]systemVersion]floatValue] >=9.0 &&self.window.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
NSLog(@"你的手机支持3D Touch!");
TianYouNetCountManager * NetCountManager = [TianYouNetCountManagersharedNetCountManager];
NetCountManager.applicationShortcutItemTitle = shortcutItem.type;
//首页
if([shortcutItem.typeisEqualToString:@"TianYou.Home"])
{
[[NSNotificationCenterdefaultCenter]postNotificationName:@"TianYou.Home"object:niluserInfo:nil];
}
//发现
if([shortcutItem.typeisEqualToString:@"TianYou.Found"])
{
[[NSNotificationCenterdefaultCenter]postNotificationName:@"TianYou.Found"object:niluserInfo:nil];
}
//活动
if([shortcutItem.typeisEqualToString:@"TianYou.Activity"])
{
[[NSNotificationCenterdefaultCenter]postNotificationName:@"TianYou.Activity"object:niluserInfo:nil];
}
//我的
if([shortcutItem.typeisEqualToString:@"TianYou.My"])
{
[[NSNotificationCenterdefaultCenter]postNotificationName:@"TianYou.My"object:niluserInfo:nil];
}
}
else
{
NSLog(@"你的手机暂不支持3D Touch!");
}
}
⬆️以上代码我都写在AppDelegate.m文件中,仅供参考和理解。
附上实际效果图如下:
UIApplicationShortcutIconType是系统给的一个枚举类型,里面有多种图标样式供我们选择:
-
UIApplicationShortcutIconTypeShare ===>
-
UIApplicationShortcutIconTypeUpdate ===>
-
UIApplicationShortcutIconTypeTime ===>
-
UIApplicationShortcutIconTypeAdd ===>
-
UIApplicationShortcutIconTypeAlarm ===>
-
UIApplicationShortcutIconTypeAudio ===>
-
UIApplicationShortcutIconTypeBookmark ===>
-
UIApplicationShortcutIconTypeCapturePhoto ===>
-
UIApplicationShortcutIconTypeCaptureVideo ===>
-
UIApplicationShortcutIconTypeCloud ===>
-
UIApplicationShortcutIconTypeCompose ===>
-
UIApplicationShortcutIconTypeConfirmation ===>
-
UIApplicationShortcutIconTypeContact ===>
-
UIApplicationShortcutIconTypeDate ===>
-
UIApplicationShortcutIconTypeFavorite ===>
-
UIApplicationShortcutIconTypeHome ===>
-
UIApplicationShortcutIconTypeInvitation ===>
-
UIApplicationShortcutIconTypeLocation ===>
-
UIApplicationShortcutIconTypeLove ===>
-
UIApplicationShortcutIconTypeMail ===>
-
UIApplicationShortcutIconTypeMarkLocation ===>
-
UIApplicationShortcutIconTypeMessage ===>
-
UIApplicationShortcutIconTypePause ===>
-
UIApplicationShortcutIconTypePlay ===>
-
UIApplicationShortcutIconTypeProhibit ===>
-
UIApplicationShortcutIconTypeSearch ===>
-
UIApplicationShortcutIconTypeTask ===>
-
UIApplicationShortcutIconTypeShuffle ===>
-
UIApplicationShortcutIconTypeTaskCompleted ===>
项目链接: https://github.com/TianYou899/3D-Touch