3DTouch桌面快捷方式

来源:互联网 发布:会议室预约系统 源码 编辑:程序博客网 时间:2024/05/22 17:33

3DTouch是iOS9,iPhone6s上新加的功能,之前只是一直有了解,但是由于设备的原因并没有很好的研究,后来购入一台6S所以就迫不及待的尝试了一下。
申明Quick Action有两种方式:静态和动态,静态是在plist文件中申明,动态则是在代码中注册,系统支持两者同时存在。但是系统限制每个app最多显示4个快捷图标,包括静态和动态。
静态的方法加入:
即在plist文件中加入UIApplicationShortcutItems标签,标签类型为NSArray,根据需要可以添加四个不同的子标签。类型为NSDictionary。然后加如所需键值对:
必填项(下面两个键值是必须设置的):UIApplicationShortcutItemType 这个键值设置一个快捷通道类型的字符串
选填项(下面这些键值不是必须设置的):UIApplicationShortcutItemSubtitle 设置标签的副标题UIApplicationShortcutItemIconType 设置标签Icon类型UIApplicationShortcutItemIconFile 设置标签的Icon文件,可以通过这个键值设置自定义文件。
这里写图片描述
动态方法加入:
在AppDelegate中加入以下方法:

- (void)configShortCutItems {    NSMutableArray *shortcutItems = [NSMutableArray array];    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"YPYD.UITouchText.home" localizedTitle:@"首页" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome] userInfo:nil];    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"YPYD.UITouchText.search" localizedTitle:@"购物车" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]                                                                             userInfo:nil];    [shortcutItems addObject:item2];    [shortcutItems addObject:item1];    [[UIApplication sharedApplication] setShortcutItems:shortcutItems];}

判断手机是否可以用3Dtouch:

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler{    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0 &&self.window.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)    {        NSLog(@"你的手机支持3D Touch!");        YPYDNetCountManager * sharedNetCountManager = [YPYDNetCountManager sharedNetCountManager];        sharedNetCountManager.applicationShortcutItemTitle = shortcutItem.type;        //首页        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.home"])        {            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.home" object:nil userInfo:nil];        }        //搜索商品        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.search"])        {            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.search" object:nil userInfo:nil];        }        //购物车        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.cart"])        {            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.cart" object:nil userInfo:nil];        }        //我的U        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.myU"])        {            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.home" object:nil userInfo:nil];        }    }    else    {        NSLog(@"你的手机暂不支持3D Touch!");    }}

这个时候通过屏幕按压已经可以出现快捷按钮了,最后就是给相应的按钮添加功能。我这里只是做了简单的跳转功能,所以在tabBarController中加入以下方法实现界面的跳转。
先添加监听方法,用来监听事件的点击事件:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationShortcutItemResponse) name:@"YPYD.UITouchText.home" object:nil];

监听的方法:

- (void)applicationShortcutItemResponse{    YPYDNetCountManager * sharedNetCountManager = [YPYDNetCountManager sharedNetCountManager];    //首页    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.home"])    {        self.tabBarController.selectedIndex = 0;        [[NSNotificationCenter defaultCenter] postNotificationName:@"UITouchText.home" object:nil userInfo:nil];    }    //搜索商品    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.search"])    {        self.self.tabBarController.selectedIndex = 2;    }    //购物车    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.cart"])    {        self.self.tabBarController.selectedIndex = 3;    }    //我的 U    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.myU"])    {        self.self.tabBarController.selectedIndex = 4;    }}

现在就可以实现点击快捷图标跳转相应控制器了。总得来说接入很方便,欢迎大家 尝试。
效果图:
这里写图片描述

0 0
原创粉丝点击